foreach loop in Multidimensional Array

G

Guest

Hi there,

I created a Multidimensional array of labels

Label[,] lblMultiArray = new Label[,] { {Label3, LblThuTotal},
{Label4,LblFriTotal} };

Now I would like to compare the values in the array, comparing the text in
Label3 and LblThuTotal and the text in Label4 and LblFriTotal.

I would like to use an foreach loop since I have to change the properties of
the label. My problem is that I do not know how to access the items on the
different positions in the Multidimensional array. How can I do this?

Thanks a lot
Chris

private string CompareHours(Label[,] lblMultiArray)
{
foreach (Label item in lblMultiArray)
{
Psuedo code
if (!Label3.Text.Equals(LblThuTotal.Text))
{
Label3.BackColor = Color.Orange
}
}
}
 
G

Guest

Hi Chris,
the foreach statement will loop through each element of the
multidimensional array allong all dimensions, so that if you have an array
like:

int[,] myIntArray = new int[,]{{1,2,3},{4,5,6}};

foreach(int i in myIntArray)
{
Console.WriteLine(i.ToString());
}

you would get 1,2,3,4,5,6

I do not know of any simpe way you can only iterate through one of the
dimensions to accomplish something like:

Label[,] labels = new Label[,]{{label1,label2},{label3,label4}};

for(int i=0; i<=xyz.GetUpperBound(0); i++)
{
Label currentLabel1 = labels[i, 0];
Label currentLabel2 = labels[i, 1];

if(currentLabel1.Text == currentLabel2.Text)
{
//we found a match
}
}

Normal for statements is probably your best bet in this case.

Mark.
 
G

Guest

Thanks Mark. That works like a dream...

Cheers

Chris

Mark R. Dawson said:
Hi Chris,
the foreach statement will loop through each element of the
multidimensional array allong all dimensions, so that if you have an array
like:

int[,] myIntArray = new int[,]{{1,2,3},{4,5,6}};

foreach(int i in myIntArray)
{
Console.WriteLine(i.ToString());
}

you would get 1,2,3,4,5,6

I do not know of any simpe way you can only iterate through one of the
dimensions to accomplish something like:

Label[,] labels = new Label[,]{{label1,label2},{label3,label4}};

for(int i=0; i<=xyz.GetUpperBound(0); i++)
{
Label currentLabel1 = labels[i, 0];
Label currentLabel2 = labels[i, 1];

if(currentLabel1.Text == currentLabel2.Text)
{
//we found a match
}
}

Normal for statements is probably your best bet in this case.

Mark.

chris said:
Hi there,

I created a Multidimensional array of labels

Label[,] lblMultiArray = new Label[,] { {Label3, LblThuTotal},
{Label4,LblFriTotal} };

Now I would like to compare the values in the array, comparing the text in
Label3 and LblThuTotal and the text in Label4 and LblFriTotal.

I would like to use an foreach loop since I have to change the properties of
the label. My problem is that I do not know how to access the items on the
different positions in the Multidimensional array. How can I do this?

Thanks a lot
Chris

private string CompareHours(Label[,] lblMultiArray)
{
foreach (Label item in lblMultiArray)
{
Psuedo code
if (!Label3.Text.Equals(LblThuTotal.Text))
{
Label3.BackColor = Color.Orange
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top