foreach in two dimensional array in C#

  • Thread starter Thread starter Gopal Krish
  • Start date Start date
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"
 
Try

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

Good Luck

Trevor
 
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"
 
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:
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"
 
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).
 
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"
 
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.
 
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.
 
Back
Top