Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create separate list for each elements in counts.

i.e if count==4 then the following list should be created

List<polygon> List1=new List<polygon>();
List<polygon> List2=new List<polygon>();
List<polygon> List3=new List<polygon>();
List<polygon> List4=new List<polygon>();



I need to concatenate the list names so that I can differentiate each list

How to create these list based on count at runtime
Posted
Updated 19-Mar-14 23:04pm
v2

I don't understand your scenario completely but perhaps an array of lists might help.
 
Share this answer
 
Comments
KUMAR619 20-Mar-14 5:01am    
I have to declare a list at runtime concatenating the count number with list name to differentiate each list


Is it possible to create such lists at runtime
Just declare array at the first time.
Then at run time add one by one list of numbers to array
But You have to collect list of numbers in run time and your list should generate according to them
 
Share this answer
 
I didn't get your scenario as well, but I think you're looking for this,
int count = 5;
List<List<string>> lists = new List<List<string>>();

for (int i = 0; i < count; i++)
{
    lists.Add(new List<string>());
}

MessageBox.Show("" + lists.Count);

-KR
 
Share this answer
 
If it's essential to name these dynamically created Lists, you could use a Dictionary<string,List<int>>:
C#
Dictionary<string, List<int>> ListDict = new Dictionary<string, List<int>>();

int listsToBuild = 10;

// build the Lists
for (int i = 1; i <= listsToBuild; i++)
{
    ListDict.Add("List" + i.ToString(), new List<int>());
}

// store a value in one of the Lists
ListDict["List1"].Add(100);

// access a value in one of the Lists
int aValue = ListDict["List1"][0];
To create a new named variable at run-time would require reflection, and I doubt you want to get into that.
 
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