VB.NET For Each Loop Examples

Foreach loop construct

The VB.NET language offers several different kinds of looping constructs; one of the most popular and clear is the For Each loop. By using For Each, you can reduce errors in your programs and also simplify syntax. The For Each loop has a specific syntax that must be followed, but is otherwise very straightforward.

These VB example programs use the For Each loop. They use For Each on String arrays.

Example 1

To start, let's examine a very simple case of the For Each loop construct in VB.NET. This example allocates an array of strings upon the managed heap when the program executes. Next, the array reference is used in the For Each statement; we declare a new variable 'fern' for every element in the array 'ferns'. Then, in the body of the loop, we can access the variable 'fern' and it represents each consecutive element in the array.

Program that uses For Each loop [VB.NET]

Module Module1
    Sub Main()
	' The input array.
	Dim ferns() As String = {"Psilotopsida", _
				 "Equisetopsida", _
				 "Marattiopsida", _
				 "Polypodiopsida"}
	' Loop over each element with For Each.
	For Each fern As String In ferns
	    Console.WriteLine(fern)
	Next
    End Sub
End Module

Output

Psilotopsida
Equisetopsida
Marattiopsida
Polypodiopsida

Reading the loop. When you read the For Each loop statement, treat the term "fern As String" as a single part; then note how "For Each" comes first and "In" near the end. For programmers familiar in other languages, the "As String" part could be confusing to the overall syntax.

Example 2

How does the For Each loop construct compare to the For loop construct in the VB.NET language? First, the For loop construct may be faster in some cases. On the other hand, as you can see in this example, the For loop has more complexity and also is longer in the source code. To use For, you need to declare an iteration variable and also specify the loop bounds.

Program that uses For Each and For [VB.NET]

Module Module1
    Sub Main()
	' The input array.
	Dim rabbits() As String = {"Forest Rabbit", _
				 "Dice's Cottontail", _
				 "Brush Rabbit", _
				 "San Jose Brush Rabbit"}
	' Loop over each element with For Each.
	For Each rabbit As String In rabbits
	    Console.WriteLine(rabbit)
	Next
	' Blank line.
	Console.WriteLine()
	' Loop with For.
	For index As Integer = 0 To rabbits.Length - 1
	    Console.WriteLine(rabbits(index))
	Next
    End Sub
End Module

Output

Forest Rabbit
Dice's Cottontail
Brush Rabbit
San Jose Brush Rabbit

Forest Rabbit
Dice's Cottontail
Brush Rabbit
San Jose Brush Rabbit

Assign iteration variable

Programming tip

In the C# language, you are not allowed to assign to the iteration variable in a "foreach" loop. The VB.NET language has no such restriction. To test this, you can assign the "rabbit" variable to another string literal in the loop body, and no exception will occur. The behavior of your program may not be as you expect it, however, in some cases.

Summary

The VB.NET programming language

By providing the powerful For Each looping construct, the VB.NET language simplifies certain common types of loops. Further, this loop can reduce bugs in the source code that might occur due to mismanagement of loop bounds in a For loop or other kind of loop. When possible, it is advisable to employ the For Each loop for this reason.

VB.NET Tutorials
.NET