Darkmage
Darkmage

Reputation: 1603

how to update in to a 2D array?

im tunning this loop and what to populate the array with the output of my methods, im not sure about that last part "array2DB[i,i] =" how shold i do this.

updated loop based on replyes

 private void BackGroundLoop()
    {
        for (int i = 1; i < 31; i++)
        {
            string txbName = "br" + i + "txt" + '3';

            TextBox txtBCont1 = (TextBox)this.Controls[txbName];

            string string1 = txtBCont1.Text.ToString();
            UpdateFormClass.runUserQuery(string1);

            array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
            array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
        }
    }

Upvotes: 1

Views: 1388

Answers (3)

Gordon
Gordon

Reputation: 4843

You have to use two for loops. One for each the x and y axis of the array.

for (int i = 0; i < 2; i++){
    for (int j = 0; j < 30; j++)
    {
        ....
        array2DB[i,j] = int.Parse(UpdateFormClass.gamleSaker.ToString())
            , int.Parse(UpdateFormClass.nyeSaker.ToString());
    }
}

Upvotes: 1

silk
silk

Reputation: 2734

I'm not 100% sure what you want to do, but you want probably this instead of your last line:

array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());

-1 in index is needed, because arrays are indexed from 0 in .NET.

Upvotes: 2

Graviton
Graviton

Reputation: 83254

This is the most you can do, without running into exception:

int[,] array2DB = new int[2, 30];
    for (int i = 0; i < 30; i++)
    {
        string txbName = "br" + i + "txt" + '3';

        TextBox txtBCont1 = (TextBox)this.Controls[txbName];

        string string1 = txtBCont1.Text.ToString();
        UpdateFormClass.runUserQuery(string1);

        array2DB[0,i] = int.Parse(UpdateFormClass.gamleSaker.ToString());
        array2DB[1,i] = int.Parse(UpdateFormClass. nyeSaker.ToString());

    }

Note that you can't have array2DB[2, *] or above because it will generate an arrayoutofbound exception.

Upvotes: 1

Related Questions