tags:

views:

75

answers:

3

im tunning this loop and what to populate teh array withteh 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());
        }
    }
+1  A: 

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.

Ngu Soon Hui
yeah i noticed and fixed the out off bounds, but the 0 and 1 in your exsample it the 0 and 1 the location? and the i the dept in the array?
Darkmage
i is the depth of the second dimension ( with the bound 30), the 0 and 1 is the only two values first dimension can have, because you declare it as such.
Ngu Soon Hui
A: 

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());
    }
}
Gordon
A: 

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.

silk
good point, so i recon the 0-1 is the location and the I is the dept then?
Darkmage
Yes, because you created the array as [2, 30].So 0-1 is position in the first dimension and i is indexing the second one.You could of course create the array as [30, 2], and then index it accordingly, depends on what you need.
silk