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 -
String tbCommand2 = "";
-
String[] tbCommands;
-
-
for (int i = 0; i < tbCommands.Length; i++)
-
{
-
tbCommand2 += tbCommands[i];
-
}
-
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... - int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
-
foreach (int i in fibarray)
-
{
-
System.Console.WriteLine(i);
-
}
... you wouldn't be able to change it to the following: - int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
-
foreach (int i in fibarray)
-
{
-
i *= 4;
-
System.Console.WriteLine(i);
-
}
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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)
|
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
|
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, "", "", "", "", ""};
|
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)
{
| |
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));
|
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...
|
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)
|
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"
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |