foreach in two dimensional array in C#

G

Gopal Krish

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"
 
G

Guest

Try

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

Good Luck

Trevor
 
G

Gopal Krish

Tried that and it throws an error ....Cannot convert type 'string' to 'string[]'

Any thoughts? Thanks


Trevor said:
Try

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

Good Luck

Trevor

Gopal Krish said:
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"
 
B

Bob Grommes

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
 
G

Guest

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

Any thoughts? Thanks


Trevor said:
Try

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

Good Luck

Trevor

Gopal Krish said:
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"
 
J

Jon Skeet [C# MVP]

Trevor said:
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).
 
G

Gopal Krish

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 said:
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 said:
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"
 
J

Jon Skeet [C# MVP]

Gopal Krish said:
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.
 
G

Gopal Krish

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 said:
Gopal Krish said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top