473,587 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert For loop into a Foreach statement?

7 New Member
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 5008
GaryTexmo
1,501 Recognized Expert Top Contributor
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
13661
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 ); } Error: Cannot pass 'vinterface' as a ref or out argument because it is a
19
1736
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)): tmp.extend(*seq)
4
4412
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 false",)) As , IIf(. Is Not Null, ("Val is true", "Val is false",)) As , (. Is Not Null, ("Val is true", "Val is false",)) As
1
1499
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
2283
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
3209
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
2370
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 validiation the code is working properly but when the condition are fulfilled the code inside the if part is not executing. The following is the code for...
2
10005
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 Number String public string MyNum(System.Object MyString) { string temp; foreach (string mylet in MyString)
2
1446
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 beside the CurveGR with heading Mineral The statement is-- if curveGR=>11.5 and curveGR <17.5 then print "dolomite"
0
7849
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...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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...
0
6626
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...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.