473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 12795
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
6
by: ubccis | last post by:
Hi. Does for each for on multidimensional arrays? For example, if one element of $in is $course_list and I want to echo all everything in course_list
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
22
by: J. Frank Parnell | last post by:
Hello, So, I was wondering how to do this: foreach($foo as $k=>$v AND $bar as $k2=>$v2){ echo '<TR><TD>$k</TD><TD>$v</TD><TD>$k2</TD><TD>$v2</TD></TR>; } Thanks,
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
3
by: JonB | last post by:
The examples I'm generally finding on the web of how to do nested loops are confusing and extremely scarce. Here's what I'm trying to do... I have an array named $data that I believe qualifies as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.