Array of lists

tcss
tcss used Ask the Experts™
on
Hi

I have a function that currently returns a List<string> of data.
The list is populated from a datagridview where the user has selected a row.

The requirement has now changed and I need to return multiple rows.
Is it possible to create an array or list fof list ojects.

I have tried
private List<string>[] RowData = new list<string>[20];
but the compler does not like it.

Is this the riogt way to go?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
May be onto somethng here
This compiled
private List<List<string>> RowData = new List<List<string>>];

Will update if I have solved my own problem
:)

Commented:
I was just wondering the exact same thing! I managed to get this to compile:

List<Array> myListOfArrays = new List<Array>();
Retired
Distinguished Expert 2017
Commented:
Hi tcss;

Here is some sample code showing how to do what you want.

Fernando
// List of List
private List<List<string>> RowData = new List<List<string>>();
 
private void button1_Click(object sender, EventArgs e)
{
    // Create a List of RowData and add it to RowData
    List<string> row = new List<string>();
    for (int i = 0; i < 11; i++)
    {
        string data = "Column No. " + i.ToString();
        row.Add(data);
    }
    RowData.Add(row);
 
    // Create another row
    row = new List<string>();
    for (int i = 100; i < 110; i++)
    {
        string data = "Column No. " + i.ToString();
        row.Add(data);
    }
    RowData.Add(row);
}
 
private void button2_Click(object sender, EventArgs e)
{
    int ListNo = 1;
    // Display the list of List
    foreach (List<string> data in RowData)
    {
        Console.WriteLine("Row number " + ListNo.ToString());
        foreach (string innerData in data)
        {
            Console.WriteLine("\tData = " + innerData);
        }
        ListNo++;
    }
}

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Use a list of objects. Such as this: List<MyNewObject>

Your new object may contain your List<string>. This will work well in the future in terms of maintenance esspecially if you ever need to add string manipulation or or access the data.

Author

Commented:
I was able to work it out but your solution demonstrates well for any other who may be looking for the same thing. Thanks for the effort in putting it togeather.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem, always glad to help out.  ;=)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial