473,809 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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 39652
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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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****@discuss ions.microsoft. com> wrote in message news:<5F******* *************** ************@mi crosoft.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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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.goo gle.com...
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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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****@discuss ions.microsoft. com> wrote in message news:<5F******* *************** ************@mi crosoft.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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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****@discuss ions.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.co m>
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******* *******@tk2msft ngp13.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.goo gle.com...
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.asp x";

menulinks[1,0] = "Credit";
menulinks[1,1] = "Credit.asp x";

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.WriteLi ne(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.co m>
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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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.WriteLi ne(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
7674
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 = (data_type**)malloc(widht*height*sizeof(data_type)+ height* sizeof(data_type*)); //allocate individual addresses for row pointers. Now that I am moving to C++,am looking for something by which I can
60
10214
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: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
22
2303
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 that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
6
2280
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 array into a subroutine, and inside it I "Redim Preserve" the array to increase the number of item in the array. I got the error "Redim statement requires an array", but arr is an array. HOw can I fix this problem ? Or, how can I do ArrayList for...
8
11831
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 way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
272
14214
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
1
4340
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 alot but did not successed accutly i want to create an yahoo api websearch application i got a sample from developer.yahoo.com for this purpose ,this sample uses unserialize/serialize php here is the code exapmle <?php // Parsing Yahoo! REST...
3
11265
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", "surname2"},{"name3", "surname3"}}; request.setAttribute("fullname",fullname); Then JSP page will use JSTL c:foreach to display it:
5
3889
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 different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file. Step 2: Create a file called...
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.