I want to create a list in a button click event...every time the button is clicked a new list should be created having different name than the previous one... Can anyone please help me out...I'm stuck on it...
-
2What do you mean by "a new list should be created having different name"? Perhaps you could post an example of something you've already tried so we can get a better feel for what you're trying to do.Dean Harding– Dean Harding2010-04-15 05:53:24 +00:00Commented Apr 15, 2010 at 5:53
-
2Lists don't have names... variables and fields have names. It sounds like you might want a list of lists, or a dictionary of lists.Marc Gravell– Marc Gravell2010-04-15 05:55:23 +00:00Commented Apr 15, 2010 at 5:55
-
public List<string> str = new List<string>(); public void check() { for (int i = 0; i < sub.Count; i++) { if (checkbx[i].Checked == true) { str.Add(checkbx[i].Text); } } } Here, the checked checkboxes will be different at all time... So, I want to store the different set of choices each time in different List<string>... The code given above is in buttonclick2 and I want to create a new list each time in buttonclick3...Shiv– Shiv2010-04-15 06:00:17 +00:00Commented Apr 15, 2010 at 6:00
-
1@ shiv09 add the code to the question by clicking edit(below the question). and try to format itGopi– Gopi2010-04-15 06:02:34 +00:00Commented Apr 15, 2010 at 6:02
-
You can try marking answers for the already asked questions by clicking on the CheckMark to the left of answers.Amsakanna– Amsakanna2010-04-15 06:41:02 +00:00Commented Apr 15, 2010 at 6:41
Add a comment
|
3 Answers
Better create List<List<string>>
List<List<string>> lls = new List<List<string>>();
Button_Click()
{
List<string> str = new List<string>();
for (int i = 0; i < sub.Count; i++)
{
if (checkbx[i].Checked == true)
{
str.Add(checkbx[i].Text);
}
}
lls.Add(str);
}
hence their names would be lls[0], lls[1],... etc
2 Comments
Shiv
hey thanks a lot...but li'l bit of confusion...how do I add items to lls[0] on clicking the button once and then to lls[1] on next button click and so on....
Amsakanna
The order it is added to the list lls is automatic. The first button click will add it to lls[0] and the second to lls[1] and so on. In your code i could see str is the list having all the checkedbox texts. Just add it to the collection lls like this: lls.Add(str) after the for loop
List objects doesn't have names, variables have names.
Creating variables dynamically is mostly useless, especially in compiled languages. What you want is a collection where you can store multiple lists, e.g. a list of lists:
public List<List<string>> str = new List<List<string>>();
public void check() {
List<string> subs = new List<string>();
for (int i = 0; i < sub.Count; i++) {
if (checkbx[i].Checked) {
subs.Add(checkbx[i].Text);
}
}
str.Add(subs);
}
Comments
Use a dictionary of lists where the key is the list name and the value is the list itself.
//Defining Dictionary
Dictionary<string, List<string>> ListDict = new Dictionary<string, List<string>>();
//Add lists in Dictionary
OnClick(string listname)
{
ListDict.Add(listname, new List<string>());
}
//Add values in List
ListDict[listname].Add("xyz");