Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help on c# list

Posted on 2012-03-12
5
Medium Priority
?
346 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 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 2000 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

824 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