Difference Between For Each and For i

SwitchBladeJones

New member
Joined
Mar 10, 2010
Messages
1
Programming Experience
1-3
I was under the assumption that VB's For Each loop was the same as a For Next loop. But when i try running the following code I do not get the expected result.

VB.NET:
       Dim foo As New List(Of String)

        For i As Integer = 0 To 4
            foo.Add(i & "th Item")
        Next

        For i As Integer = 0 To foo.Count - 1
            foo(i) &= "-AddedText"            
        Next

        For Each strFoo As String In foo
            strFoo &= "-MoreText"
        Next

        For Each strItem As String In foo
            MsgBox(strItem)
        Next


I expect each iteration of the MsgBox to show <#> th Item-AddedText-MoreText.

But it doesn't. It shows <#> th Item-AddedText.

Why does the for each loop not assign values but the For i loop does? So we still have to use the For i loop when we are changing/assigning values but we can use the For Each loop when just viewing data?


Thanks!
 
A For Each loop doesn't have any index numbers, it simply starts at the beginning and ends after the last item. If there are no items then nothing inside the loop runs.

A For...Next specifies where it starts and where it ends, you can also skip indicies using the Step keywork, of which you can't do in a For Each.

For Each is typically used to process items in an array (single dimension at a time) and collections.
 
For-Each can't be used to assign a new instance to the current array/collection item, it can only be used to look them up. The iterator variable refers to the object, not the array, so while you in given situations can modify the object you can not swap it with a different one through that variable.
SwitchBladeJones said:
strFoo &= "-MoreText"
This code is short for this:
VB.NET:
strFoo = strFoo & "-MoreText"
and it can be read as: "add the contents of strFoo variable to "-MoreText" and assign the result (a new string) to the strFoo variable", in other words it is not a modification of the object.
 
The For Each loop is limited in what it can do, as has been suggested, but it more succinct if that's all you actually want. A For Each loop gives you direct access to each item in a list in turn,

A For loop on the other hand, doesn't have to have anything to do with a list at all. It simply starts a some number and finishes at another number. Those numbers might represent the indexes of items in a list but they might not. For instance, here's a For loop:
VB.NET:
For index As Integer = 0 To myList.Count - 1
    MessageBox.Show(myList(index).ToString())
Next
Now here's the equivalent For Each loop:
VB.NET:
For Each item As Object In myList
    MessageBox.Show(item.ToString())
Next
As you can see, when you want to get and use each item in a list, the For each loop is more succinct. Now look at this For loop:
VB.NET:
For month As Integer = 1 To 12
    MessageBox.Show(New Date(2010, month, 1).DayOfWeek.ToString())
Next
That will show you in turn what day of the week each month of this year is. How would you write a For Each loop to do that? The answer is that you couldn't, because there's no list to get each item of. You'd have to write extra code to create a list in the first place, which makes the code longer and less clear.
 
Back
Top