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.
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!
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!