Scott Solmer
Scott Solmer

Reputation: 3897

String array declaration using for loop

I need to declare 32 string arrays containing arrays in C#. Writing it all out works OK similar to this:

string[] row1 = new string[] { "NO.", "DATA BIN", "BIT2", "BIT1", };
string[] row2 = new string[] { "1", DataBin[1], BitLabels[1, 1], BitLabels[0, 1], };
string[] row3 = new string[] { "2", DataBin[2], BitLabels[1, 2], BitLabels[0, 2], };
string[] row4 = new string[] { "3", DataBin[3], BitLabels[1, 3], BitLabels[0, 3], }; 

But it would be so much cleaner / easier if I could create them like this:

string[] row1 = new string[] { "NO.", "DATA BIN", "BIT2", "BIT1", };
for (int i = 2; i < 33; i++)
{
string[] row(i) = new string[] { Convert.ToString(i), DataBin[i], BitLabels[1, i], BitLabels[0, i], };
}

The problem is that I can't index the instance name (row1 for example)

This is for a DataGridView, so I also need to have something to take care of the following:

object[] rows = new object[] { row1 , row2 , row3 , row4 , row5 , row6 , row7 , row8 , row9 ,
                               row10 , row11 , row12 , row13 , row14 , row15 , row16 , row17 ,
                               row18 , row19 , row20 , row21 , row22 , row23 , row24 , row25 ,
                               row26 , row27 , row28 , row29 , row30 , row31 , row32 , row33 };            
foreach (string[] rowArray in rows)
        {
            myDataGridView.Rows.Add(rowArray);
        }

Any suggestions? (sorry many edits for clarity)

Upvotes: 0

Views: 237

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273169

You can use a multidimensional ([,]) or a jagged ([][]) array.

Based on your example I would go for jagged (array of array):

string[][] rows = new string[32][];

for (int i = 0; i < rows.Length; i++)
{
    int n = i+1;  // you seem to use 1 as origin
    rows [i] = new string[] { n.ToString(), DataBin[n], ... };
}

Edit: This is for a DataGridView, so ...

So you can't actually use an array (for the columns), you will need a proper class. With property-names to bind to. You don't need array for the rows either (to restrained).

An easy way to get from where you are to where you need to be is to use an anonymous class:

(Borrowing a piece from Tim)

var rows = Enumerable.Range(1, 32)
     .Select(i => new {   // this is the anon class
         Index = i.ToString(),   // on-the-fly property 'Index'
         Text1 = DataBin[i], 
         Text2 = BitLabels[2, i], 
         Text3 = BitLabels[1, i],  /* etc */ }
       );

And then you can bind your columns to Index, Text1, Text2, etc

Upvotes: 5

JeremiahDotNet
JeremiahDotNet

Reputation: 910

It looks like you've almost got it. The array answer is a good one. Another possible approach would be to use a list. You can convert the list to an array later if needed. As you can see the code is clean and easy to follow.

var rows = new List<string[]>();
for (int i = 0; i < 32; i++)
{
    var row = new string[] { Convert.ToString(i), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i], };
    rows.Add(row);
}

// use rows.ToArray() if you need it to be an array or arrays later.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460028

Maybe you want to use Linq:

string[][] rows = Enumerable.Range(1, 32)
    .Select(i => new [] { i.ToString(), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i], })
    .ToArray();

Upvotes: 3

Felipe Oriani
Felipe Oriani

Reputation: 38598

You could use a List<string[]>, for sample:

var rows = new List<string[]>();

for (int i = 0; i < 32; i++)
{
   rows.Add(new string[] { Convert.ToString(i), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i] });
}

After it, could use the rows object and access by

rows[0][3]

Where the first index is a item of the List, and second index is the index of the array.

You also could call the ToArray() and get a array of string[].

Upvotes: 2

Related Questions