0

I have a list of words:

List<string> listofwords;

public void list()
{
   listofwords = new List<string>();
   listofwords.Add("tackle");
   listofwords.Add("hinder");
   listofwords.Add("mentor");
}

Each word contains multiple data in it:

public List<string> tackle;
public List<string> hinder;
public List<string> mentor;

public void A()
{  
   mentor = new List<string>();
   mentor.Add("tern");
   mentor.Add("tome");
   mentor.Add("tone");

   tackle = new List<string>();
   tackle.Add("alce");
   tackle.Add("cake");
   tackle.Add("calk");
   tackle.Add("cate");
   tackle.Add("kale");
   tackle.Add("lace");

   hinder = new List<string>();
   hinder.Add("deni");
   hinder.Add("dine");
 }

In the button click event, I want to check a list. The list's name is stored in a string variable val:

private void button1_Click(object sender, EventArgs e)
{
   if (val.Contains(textBox1.Text )))
      val.Remove(textBox1.Text);
}

How do I use val to check the list?

1
  • List.Contains should work. Are you getting an error message? Commented Apr 19, 2013 at 19:50

1 Answer 1

6

Consider using a Dictionary for this problem. The key type should be String and the value type should be List<String>.

Your code would look something like this:

_listOfWords = new Dictionary<String, List<String>>();
_listOfWords.Add("mentor", new List<String>
                                    {
                                        "tern",
                                        "tome",
                                        "tone"
                                    });
_listOfWords.Add("tackle", new List<String>
                                    {
                                        "alce",
                                        "cake",
                                        // etc.
                                    });

You can then get the "data" for words from the dictionary. List<String> list = _listOfWords[val]; and do with it as you please.

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

2 Comments

You really should consider giving a code sample detailing what your answer is. -1 until you do.
I didn't see it when I gave the down vote. +1 now that you added a code sample.

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.