Solved

Help on c# list

Posted on 2012-03-12
5
339 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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