Solved

Help on c# list

Posted on 2012-03-12
5
340 Views
Last Modified: 2012-06-21
Hi,

I have a array of list & I am trying to add items to the array of list & getting object reference error. Could you see if anything wrong here.

         List<string>[] list = new List<string>[4];

   
        list[0].Add("test3");
        list[0].Add("test4");
 
        list[1].Add("test1");
        list[1].Add("test2");

      Messagebox.Show(list[0].ToArray());
     Messagebox.Show(list[1].ToArray());
0
Comment
Question by:r_pat72
  • 3
5 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 37712688
OK, you have created an array of lists but you haven't created lists in this array!
You have to create them explicitly.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 37712705
Try this:

        private void button2_Click(object sender, EventArgs e)
        {
            List<string>[] list = new List<string>[4];

            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new List<string>();
            }

            list[0].Add("test3");
            list[0].Add("test4");

            list[1].Add("test1");
            list[1].Add("test2");

            //MessageBox.Show(list[0].ToArray().ToString());
            MessageBox.Show(list[1].ToArray().ToString());

            // see in output window:
            for (int i = 0; i < list.Length; i++)
            {
                int j = 0;
                List<string> lst = list[i];
                foreach (string s in lst)
                {
                    System.Diagnostics.Debug.WriteLine("List[" + i + "][" + j + "] = " + list[i][j]);
                    j++;
                }
            }
        }

Open in new window

0
 

Author Comment

by:r_pat72
ID: 37712728
Do I have to do something like this


List<string>[] list = new List<string>[4];

       list[0] =   new List<string>();
        list[0].Add("test3");
        list[0].Add("test4");
 
       list[1] =   new List<string>();
        list[1].Add("test1");
        list[1].Add("test2");

      Messagebox.Show(list[0].ToArray());
     Messagebox.Show(list[1].ToArray());
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 500 total points
ID: 37712740
Yes, you need to do that.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37719657
You should be able to use initializer syntax, like this:


            List<string>[] list = new List<string>[]
            {
                new List<string>{"test 1-1", "test 1-2", "test 1-3"},
                new List<string>{"test 2-1", "test 2-2", "test 2-3"}
            };

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question