For Each Loop

The For/Each construct is a variation of the standard For loop, where you are able to iterate over the contents of an array without the need to explicitly monitor the container's upper limit (as in the case of a traditional For/Next loop). Assume you have defined an array of String objects and wish to print each item to the command window (VB 2008 array syntax will be fully examined in the next chapter). In the following code snippet, note that the For Each statement can define the type of item iterated over directly within the statement:

Sub Main() Dim myStrings() As String = _ {"Fun", "with", "VB 2008"}

For Each str As String In myStrings

Console.WriteLine(str) Next End Sub or on discrete lines of code:

Sub Main() Dim myStrings() As String = _ {"Fun", "with", "VB 2008"}

Was this article helpful?

0 0

Post a comment