Yet my answer is not the best depending on what the OP really wants to achieve. It's not very clear. But in any case wrapping the array inside a class seems the most extensible approach.
...
For one there's the question of whether ArrayList is or not the best option. I was being lenient and didn't feel like getting into much detail at 5am, which is when I posted that Now that I've freshen up, let's see...
ArrayList rarely is a good option since 2.0. The one big problem is it doesn't enforce strong typing. Once I define an ArrayList object I can put anything in there:
Code:
ArrayList arr = new ArrayList();
arr.Add(new int());
arr.Add(new Car());
arr.Add(Invoices);
That's... ugh!
Sure, since I'm wrapping it inside a class I can create the logic to make sure only objects of the same type are inserted. But then that's what the generic types are all about.
Code:
List<int> arr = new List<int>();
arr.Add(new int());
arr.Add(new Car()); // compile-time error
List<T> is an implementation of the IList<T> interface -- both defined under System.Collections.Generic -- And is aimed at mimicking ArrayList functionality. The System.Collections.Generic namespace is pretty much akin to our C++ STL implementation.
On the other hand, we can simply ignore these classes and go back to basics with the Array class. According to the OP the only requirement is that the size must be set only once. Well, that's pretty much what Array does
...
So ArrayList (and the other non-generic containers under System.Collections) is something that should not be used. Either the generic types if we need dynamic allocation, or the base Array type if we only need a fixed size array.
Other than that I would need more details from the OP. Simply put there may not even be a need for a wrapper class. It sure seems so:
Originally Posted by
OP
I've been doing some digging around and I wonder if I can now say
Code:
foreach (myClass instance in myArray){
instance = new myClass(somethingToGoInEachConstructor);
}
What do you think?
Sure, you can. The only thing you want to make sure is that myClass constructor(s) are defined. For instance:
Code:
Class Computer
{
private string brand_;
private string model_;
private long memory_;
public Computer() { } // default constructor. Members are set to default values
public Computer(string brand, string model, long memory)
{ // custom constructor. All members are set to user defined values
brand_ = brand;
model_ = model;
memory_ = memory;
}
}
With that you can now create an array of Computer objects and populate it according to the constructors you have defined:
Code:
Computer[] myComputers = new Computer[4];
myComputers[0] = new Computer() // new computer with default constructor;
myComputers[1] = new Computer("HP", "Prolient", 4000000000); // new computer with user-defined constructor
Computer dad_computer = new Computer();
Computer mom_computer = new Computer("Toshiba", "Satellite", 3000000000);
myComputers[2] = dad_computer; // computer added from existing object
myComputers[3] = mom computer; // computer added from existing object