471,599 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

foreach loop in Multidimensional Array

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
}
}
}

Nov 17 '05 #1
2 12580
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" wrote:
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
}
}
}

Nov 17 '05 #2
Thanks Mark. That works like a dream...

Cheers

Chris

"Mark R. Dawson" wrote:
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" wrote:
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
}
}
}

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randell D. | last post: by
6 posts views Thread by ubccis | last post: by
32 posts views Thread by Joe Rattz | last post: by
4 posts views Thread by Sjoerd | last post: by
29 posts views Thread by Jon Slaughter | last post: by
9 posts views Thread by Nathan Sokalski | last post: by
22 posts views Thread by J. Frank Parnell | last post: by
7 posts views Thread by Osiris | last post: by
3 posts views Thread by JonB | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.