views:

488

answers:

9

If you have a for loop such as:

For Each s As String In stringList
    ..do stuff..
Next

is there some sort of (hidden) iterator or do I have to add an external integer to keep count:

Dim i As Integer
For Each s As String In stringList
    ..do stuff..
    i += 1
Next
+2  A: 

A foreach does not require another counter. It iterates through each string in your list (assuming stringList in your example is just what it sounds like :) ). Unless there is some reason you want to stop doing something to each string in your list (i.e. only process the first 3 strings). That would be the only reason to have a counter inside your foreach loop. I just reread the OP. The other reason is to know how many strings you have processed.

Hope this helps! Theresa

Theresa
+8  A: 

There is no hidden iterator, you would have to use the code in your second example or revert back to a regular for loop.

Garry Shutler
Don't forget to initialize i.
Martinho Fernandes
VB initializes integers to zero automatically in most cases, including this one.
Michael Haren
A: 

A for-each loop will keep track of the enumerator for you.

ForEach s As String In new string[] {"one","two","three"}
    Console.WriteLine(s);
Next

(Sorry about the bastardisation of VB and C#, I'm a curly brackets kind of guy - probably best to think of it as pseudo-code)

The code above would output

one

two

three

if it would compile. :)

ZombieSheep
A: 

There is no hidden iterator.

The variable i in your example is redundant.

PS: If you do need i to keep track of something, I'd initialise it before I'd rely on it.

Galwegian
A: 

You definitely don't need to provide your own external integer.

Even so, there is no hidden counter. There is a hidden enumerator. The difference is that not everything that can be iterated is accessible by index. Some times you can't get the next item in a sequence until you've calculated the previous item. So an enumerator may use a state machine that tracks your current position and returns the next item for each iteration of the loop, rather than using a counter.

Joel Coehoorn
A: 

foreach loops are implemented using an Enumerator which keeps track of the count. If you want to know how many loops you've gone through you need to use a for loop or keep track of the count yourself.

JoshBerke
A: 

Part of the magic of "For Each" is that you don't need to track the loop incrementor yourself. It's done for you. You may read documentation refer to the interface "IEnumerable" and say it must implement "GetEnumerator()" which is true. The GetEnumerator() function provides this magic.

I think that is correct. I'm sure someone will correct me if otherwise.

Chris
A: 

From an elegant code view it would be nice if the enumerator was available with .First() and .Last() methods.

John Paul Jones
A: 

Django version 1.0.2 final ...

To enumerate:

{% for item in items %}
    <li>{{ forloop.counter }}) {{ item }}</li>
{% endfor %}

Try this to apply a 'rule' to the first item in a loop:

    {% if forloop.first %}Starting the loop{% endif %}

Try this to exclude the last item:

    {% if not forloop.last %}, {% endif %}

... the last example is great for comma separated lists.

Paul G Petty
What does Django have to do with it? This a VB.Net question.
Georg Fritzsche