473,398 Members | 2,404 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,398 software developers and data experts.

How to convert For loop into a Foreach statement?

Hi all, I think this question should be easy for some of you guys, but I really dont know whats the equivalent, I would like to convert my For loop inot a ForEach statement, anybody familiar with this ?
Thanks

Expand|Select|Wrap|Line Numbers
  1. String tbCommand2 = "";
  2. String[] tbCommands;
  3.  
  4. for (int i = 0; i < tbCommands.Length; i++)
  5. {
  6.     tbCommand2 += tbCommands[i];
  7. }
  8.  
Nov 19 '10 #1
1 4983
GaryTexmo
1,501 Expert 1GB
Have a read here:

http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

A foreach loop works on an object's enumerator. All arrays and lists have these built in (you can add them to your own types too, check out the IEnumerable interface). Anyway, that link contains all you need to know in order to convert your loop (except you're using strings instead of integers), take a crack at it and let me know if you run into troubles!

Additionally, it's worth noting that when using a foreach loop, you're not allowed to modify the value of the iteration variable. So in the example from the link...

Expand|Select|Wrap|Line Numbers
  1.         int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
  2.         foreach (int i in fibarray)
  3.         {
  4.             System.Console.WriteLine(i);
  5.         }
... you wouldn't be able to change it to the following:

Expand|Select|Wrap|Line Numbers
  1.         int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
  2.         foreach (int i in fibarray)
  3.         {
  4.             i *= 4;
  5.             System.Console.WriteLine(i);
  6.         }
This doesn't mean don't use a foreach loop, they're great and I love them, it just means know how they work and what you need for your task :)

Good luck!
Nov 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ano | last post by:
I received this error when I used "ref" in foreach statement. Dictionary<string, object> oInterfaces; foreach (clsInterface vinterface in oInterfaces.Values) { func1( ref vinterface ); } ...
19
by: bvdp | last post by:
Please help my poor brain :) Every time I try to do a list comprehension I find I just don't comprehend ... Anyway, I have the following bit of code: seq = tmp = for a in range(len(seq)):...
4
by: ultraJeep421 | last post by:
Hello I am trying to write a SQL statement based on a SQL statement from the QBE grid. The QBE query takes values from multiple tables. SELECT IIf(. Is Not Null, ("Val is true", "Val is...
1
by: samimmu | last post by:
this is the code down is my code, i use c statement but i want to convert it to c++ statement, anyone can help: struct dtbs blankdtbs = { 0, "", "", "", "", ""};
6
by: David zha0 | last post by:
Hi, I found these doesn't work: private void button1_Click(object sender, EventArgs e) { string tmpstr = new string { "david", "bill", "mike" }; foreach (string tmp in tmpstr) {
3
by: jmDesktop | last post by:
I have this: foreach (DataRow row in myDS.Tables.Rows) { int ctr=0; //counter for child node //create a parent node for each new row trvRetrieved.Nodes.Add(new TreeNode(myIdNumber));
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in...
2
boss32178
by: boss32178 | last post by:
Ok I get this error when i try to run the web app. foreach statement cannot operte on variables of type 'object' does not contain a public definition for 'GetEnumerator' here is the code #region...
2
by: Jim Rockett | last post by:
The curveGR is a column of numbers The numbers in the statement are values to match against the curve to give the name of the mineral in this case dolomite. Dolomite is to be shown in a column...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.