How to: Run Several Statements for Each Element in a Collection or Array
The For Each statement construction is similar to the For...Next loop, but it runs the statement block for each element in a collection, instead of a specified number of times. A For Each...Next loop uses an element variable that represents a different element of the collection during each repetition of the loop.
Repeating a Statement Block for a Collection
To run a group of statements for each element in a collection
Identify the collection on which to run the statement block, and use the For Each...Next Statement (Visual Basic) to specify both the element variable and the collection.
For Each thisControl As System.Windows.Forms.Control In thisForm.Controls
If the element variable is not declared outside the loop, you can use the As clause to declare it as part of the For Each statement.
Visual Basic automatically substitutes a different element of the collection for each iteration.
Complete the For Each...Next construction with a Next statement following the last statement to be repeated. You can specify the element variable in the Next statement.
Sub lightBlueBackground(ByVal thisForm As System.Windows.Forms.Form) For Each thisControl As System.Windows.Forms.Control In thisForm.Controls thisControl.BackColor = System.Drawing.Color.LightBlue Next thisControl End Sub
You can read the elements of the collection, but you cannot make any changes to the collection itself, such as adding, deleting, or replacing any element. However, if an element is a reference type, you can access and set its members. Because each Control element is a reference type, the code in the preceding example can change its BackColor property.
Repeating a Statement Block for an Array
You can also iterate through an array with a For Each...Next loop. However, as with collections, you can only read the array elements, not change them.
To run a group of statements for each element of an array
Use the For Each statement to specify both the element variable and the array. Do not follow the array name with parentheses.
Visual Basic treats the array the same way it treats a collection.
Follow the statement block with a Next statement. You can specify the element variable in the Next statement.
The following procedure is intended to find the sum of the elements of an array and reset each element to zero.
Function sumAndReset(ByRef numbers() As Integer) As Integer Dim sum As Integer = 0 For Each elt As Integer In numbers sum += elt ' The following statement works only on the local copy ' of the array, not on the original array. elt = 0 Next elt Return sum End Function
The summation works as intended because it relies only on reading the elements. However, the resetting to zero does not work as intended, because only the local copy of each element is reset to zero, while the original element is left unchanged in the original array.
See Also
Tasks
How to: Transfer Control Out of a Control Structure
How to: Run Several Statements Repeatedly
How to: Improve the Performance of a Loop
How to: Skip to the Next Iteration of a Loop
Reference
For...Next Statement (Visual Basic)
Concepts
Decision Structures
Loop Structures
Other Control Structures
Nested Control Structures