Question about using a FOREACH loop????

  • Thread starter Thread starter rhaazy
  • Start date Start date
R

rhaazy

If I want to use a FOREACH loop on an array of strings, but didn't want
to include the first member,how would I do this???

Psuedo Code:::

foreach(string a in myStringArray (where a.index>0))
{

some stuff....

}
 
Just keep track if you are on the first one and "continue;"?

Actually, if it is (as you say) a straight array (not an ArrayList,
List<string> or other), then indexer access is very efficient, so you could
just for(int i=1;...) instead of the usual for(int i=0;...).

Anything else is just introducing complexity... to my mind, at least...

Marc
 
rhaazy said:
If I want to use a FOREACH loop on an array of strings, but didn't want
to include the first member,how would I do this???

Psuedo Code:::

foreach(string a in myStringArray (where a.index>0))

I don't believe it's possible within the foreach loop syntax. Some
alternate suggestions:

int length = myStringArray.Length;
for (int i = 1; i < length; i++)
{
// do stuff with myStringArray
}

Or:

bool first = true;
foreach(string a in myStringArray)
{
if (first)
first = false;
else
// do stuff
}
 
Thanks for the quick reply, I figured it would have to be done with a
for loop so I quickly switched my method over, but thanks for the help!

Mini-Tools Timm said:
rhaazy said:
If I want to use a FOREACH loop on an array of strings, but didn't want
to include the first member,how would I do this???

Psuedo Code:::

foreach(string a in myStringArray (where a.index>0))

I don't believe it's possible within the foreach loop syntax. Some
alternate suggestions:

int length = myStringArray.Length;
for (int i = 1; i < length; i++)
{
// do stuff with myStringArray
}

Or:

bool first = true;
foreach(string a in myStringArray)
{
if (first)
first = false;
else
// do stuff
}

--
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com
 
Back
Top