How to: Iterate through a Collection in Visual Basic

You can use a For Each loop to iterate through every element of a collection.

Example

The following example uses the For Each...Next Statement (Visual Basic) to access every item in a collection.

Dim testCollection As New Microsoft.VisualBasic.Collection()
' The collection is empty until you add one or more items to it.
For Each collectionItem As Object In testCollection
    ' Perform desired processing on each item.
Next collectionItem

Compiling the Code

This example requires:

  • Access to the System namespace.

See Also

Tasks

How to: Add, Delete, and Retrieve Items of a Collection

Concepts

Collections in Visual Basic

Reference

For Each...Next Statement (Visual Basic)