This documentation is archived and is not being maintained.

How to: Iterate through a Collection in Visual Basic

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

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

This example requires:

  • Access to the System namespace.