By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,199 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,199 IT Pros & Developers. It's quick & easy.

foreach loop in Multidimensional Array

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.