Hi Everyone,
I was wondering if someone could help me. I have a class that I need to instantiate multiple times depending on how much info there is. I want to be able to do something like.......
myClass class1 = new MyClass();
myClass class2 = new MyClass();
myClass class3 = new MyClass();
...but I want to be able to set the class number, for example, something like....
int i = 0;
myClass class(i) = new MyClass(); //class(0)
i++;
myClass class(i) = new MyClass; //class(1);
i++;
Is there any way this can be done?
Any help would be appreciated. Thanks in advance!
Jay
JayPosted Dec 3, 2008, 4:34 PM
Perfect Alan! I was able to get it to work with my situation. Thanks for your help...Again!!!! I really appreciate it!
Jay
AlanPosted Dec 3, 2008, 2:30 PM
If you don't know in advance how many objects are to be created, I'd suggest you use an ArrayList or, if you have .NET 2.0 or later, a generic List of myClass objects.
These are like dynamic arrays which can increase their capacity as new objects are added and each object can be referenced by its index. For example:
List list = new List();
list.Add (new myClass()); // index 0
list.Add (new myClass()); // index 1
list.Add (new myClass()); // index 2
To get the last object added you'd use list[2].