473,396 Members | 1,877 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,396 software developers and data experts.

foreach in two dimensional array in C#

Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"
Nov 16 '05 #1
8 39621
Try

foreach(string[] strArray in menulinks)
label1.Text += strArray[0];

Good Luck

Trevor

"Gopal Krish" wrote:
Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"

Nov 16 '05 #2
Tried that and it throws an error ....Cannot convert type 'string' to 'string[]'

Any thoughts? Thanks
Trevor <Tr****@discussions.microsoft.com> wrote in message news:<5F**********************************@microso ft.com>...
Try

foreach(string[] strArray in menulinks)
label1.Text += strArray[0];

Good Luck

Trevor

"Gopal Krish" wrote:
Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"

Nov 16 '05 #3
I think you really need a for loop for this kind of thing. At present, it's
slightly faster than a foreach anyway (not that this probably matters in
this context).

--Bob

"Gopal Krish" <ge*****@yahoo.com> wrote in message
news:8b**************************@posting.google.c om...
Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"

Nov 16 '05 #4


"Gopal Krish" wrote:
Tried that and it throws an error ....Cannot convert type 'string' to 'string[]'

Any thoughts? Thanks
Trevor <Tr****@discussions.microsoft.com> wrote in message news:<5F**********************************@microso ft.com>...
Try

foreach(string[] strArray in menulinks)
label1.Text += strArray[0];

Good Luck

Trevor

"Gopal Krish" wrote:
Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"

Nov 16 '05 #5
Trevor <Tr****@discussions.microsoft.com> wrote:
foreach(string[] strArray in menulinks)
label1.Text += strArray[0];


The reason that won't work is that this is a rectangular array, not a
jagged array (an array of arrays).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
for loop works..its no problem.

I just wanted to do it the object oriented way. if foreach works for
one dimensional array then I believe it should also work for two
dimensions...

I searched Google and other sites but could not find any answers that
work.


"Bob Grommes" <bo*@bobgrommes.com> wrote in message news:<ec**************@tk2msftngp13.phx.gbl>...
I think you really need a for loop for this kind of thing. At present, it's
slightly faster than a foreach anyway (not that this probably matters in
this context).

--Bob

"Gopal Krish" <ge*****@yahoo.com> wrote in message
news:8b**************************@posting.google.c om...
Any sample code that uses foreach statement to walkthrough a two
dimensional array? My requirement is to process data in only one
dimension (say column 0 in each row of the array)

Example
menulinks[0,0] = "My Home";
menulinks[0,1] = "MyHome.aspx";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.aspx";

menulinks[2,0] = "Debit";
menulinks[2,1] = "debit.aspx";
foreach (string menuitem in menulinks)
{
Label1.Text += " " + menuitem
}

Current Output in Label1.Text
" My Home MyHome.aspx Cerdit Credit.aspx Debit debit.aspx"

What I needed
" My Home Cerdit Debit"

Nov 16 '05 #7
Gopal Krish <ge*****@yahoo.com> wrote:
for loop works..its no problem.

I just wanted to do it the object oriented way. if foreach works for
one dimensional array then I believe it should also work for two
dimensions...


foreach *does* work on rectangular arrays - it gives you every element
of them:

using System;

class Test
{
static void Main()
{
int[,] array = new int[,]{{0,1,2},{3,4,5},{6,7,8}};

foreach (int x in array)
{
Console.WriteLine(x);
}
}
}

foreach gives every element of the collection you run it over. In your
case, you didn't *want* every element in the array, so foreach is
inappropriate.

You could fairly easily (and more easily with C# 2.0) write something
which, given a two-dimensional array, will return an enumerator which
iterates over just one dimension of it, using a fixed value for the
other dimension - but that's not something which is in the framework.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Thanks Jon,

As you said, foreach works on two dimensional array but for every
element and my requirement was to iterate thru one dimension only.

I now understand that its not appropriate to use foreach to navigate
thru a single dimension in a two dimensional array.

Thanks everyone for your thoughts...

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Gopal Krish <ge*****@yahoo.com> wrote:
for loop works..its no problem.

I just wanted to do it the object oriented way. if foreach works for
one dimensional array then I believe it should also work for two
dimensions...


foreach *does* work on rectangular arrays - it gives you every element
of them:

using System;

class Test
{
static void Main()
{
int[,] array = new int[,]{{0,1,2},{3,4,5},{6,7,8}};

foreach (int x in array)
{
Console.WriteLine(x);
}
}
}

foreach gives every element of the collection you run it over. In your
case, you didn't *want* every element in the array, so foreach is
inappropriate.

You could fairly easily (and more easily with C# 2.0) write something
which, given a two-dimensional array, will return an enumerator which
iterates over just one dimension of it, using a fixed value for the
other dimension - but that's not something which is in the framework.

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
6
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
1
khalidbaloch
by: khalidbaloch | last post by:
hi every one, how are you folf , hope fine dear Friends i want to get values of multi-dimensional arrays using foreach loop and after that print out the html using an other while loop , i tried...
3
by: lisagh81 | last post by:
Hello I have a servlet that creates results in a two dimentional array and then uses request.setAttribute to store it. for example : String fullname = {{"name1", "surname1"},{"name2",...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.