Solved

Help on c# list

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now