Connecting Tech Pros Worldwide Forums | Help | Site Map

foreach loop in Multidimensional Array

chris
Guest
 
Posts: n/a
#1: Nov 17 '05
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
}
}
}


Mark R. Dawson
Guest
 
Posts: n/a
#2: Nov 17 '05

re: foreach loop in Multidimensional Array


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:
[color=blue]
> 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
> }
> }
> }
>[/color]
chris
Guest
 
Posts: n/a
#3: Nov 17 '05

re: foreach loop in Multidimensional Array


Thanks Mark. That works like a dream...

Cheers

Chris

"Mark R. Dawson" wrote:
[color=blue]
> 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:
>[color=green]
> > 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
> > }
> > }
> > }
> >[/color][/color]
Closed Thread