Creating an array of a class Objects

Runtime_error

Active member
Joined
Aug 5, 2003
Messages
38
eloooooooooooo, could neone please help me figure out how i can create an array of objects of a class whose constructer requires a parameter?

Like

myClass[] ObjArray = new myclass[10](Parameter);?? ?????


if the Class didnt need any parameters then it would be easy like

myClass[] ObjArray = new myClass[10];

but i am stuck.....

i want to reference my ObjArray like this

ObjArray[0].Field :-\
 
You need to loop through and instantiate each one. Just doing this

C#:
myClass[] ObjArray = new myClass[10];

Doesnt instantiate the elements either, it just dimensions the array.
 
I know,I tried that before, the code compiled but didnt work.

I actually have solved the problem by using a Collections class.

But i wud still like to know how to create an array of the objects of a class which requires a parameter in the constructor.


Here is my solution , just the main part

public class ClientCollection : System.Collections.CollectionBase
{
public ClientCollection(Policy MyPolicy)
{

for(int i=0; i< MyPolicy.PeopleCount;i++)
{
CClient myClient = new CClient(MyPolicy);
this.Add(myClient);
myClient = null;
}
}
......................
 
Ooooh Divil thats given me an idea.

So if i create one Dimenstional Array of the class then instantiate each element of the array using a loop, that would work rite?

myClass[] ObjArray = new myClass[10];

for(int i=0;i<10; i++)
{
ObjArray = new myClass(param);
}

makes sense but i am using collections now, might check this one out later to see if it works or not.
 
Back
Top