Solved

Help on c# list

Posted on 2012-03-12
5
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 30

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 30

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 30

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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