Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Class Master .which contain parameterize constructor named
Master(name,account,password)
and i want to create an array which hold the new object of class master
like:
var [i] master=new master[](new Master(name,account,password);
for each value of "i " I want to create new Master object.
Please tell me how can i do that?
Posted

Simply use a List<Master>:
C#
List<Master> list = new List<Master>();
list.Add(new Master(name, account, password));

var is not a type in itself ; you cannot create an array of var.
 
Share this answer
 
v2
Hey there,

Here is how you do it:
C#
//create array
var masterArr =new master[5];
//add objects
masterArr[0] = new master(name,account,password);


You can do this if you know the size of your array, otherwise you can do what phil suggested.

Hope it helps.
Azee...
 
Share this answer
 
C#
Master[] masterArray = new Master[i];
for (int index = 0; index < i; index++)
{
    masterArray[i] = new Master(/* parameters associated with index value */);
}

Using the actual type rather than var is much better practice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900