0

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...

5
  • 2
    What 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. Commented Apr 15, 2010 at 5:53
  • 2
    Lists don't have names... variables and fields have names. It sounds like you might want a list of lists, or a dictionary of lists. Commented 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... Commented Apr 15, 2010 at 6:00
  • 1
    @ shiv09 add the code to the question by clicking edit(below the question). and try to format it Commented 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. Commented Apr 15, 2010 at 6:41

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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....
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
0

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

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.