| ArrayList: GetEnumerator |
Returns an enumerator for an ArrayList.
Public Function GetEnumerator( Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant ) As Object
This enumerator can be used in For..Each loops. To access the optional parameters, the GetEnumerator must be called instead of simply passing the object to the For..Each. This allows for a portion of the list to be enumerated.
Private Sub Main()
Dim list As New ArrayList
list.Add "The"
list.Add "quick"
list.Add "brown"
list.Add "fox"
' Display the words using the standard For..Each method.
Console.WriteLine "Using For..Each"
IterateUsingForEach list
' Display the words using the enumerator manually.
Console.WriteLine "Using an Enumerator"
IterateUsingEnumerator list
' Display a subset of the list using For..Each.
Console.WriteLine "Itertate Subset"
IterateSubset list, 1, 2
End Sub
Private Sub IterateUsingForEach(ByVal list As ArrayList)
Dim word As Variant
For Each word In list
Console.WriteValue vbTab & word
Next word
Console.WriteLine
End Sub
Private Sub IterateUsingEnumerator(ByVal list As ArrayList)
Dim en As IEnumerator
Set en = list.GetEnumerator
Do While en.MoveNext
Console.WriteValue vbTab & en.Current
Loop
Console.WriteLine
End Sub
Private Sub IterateSubset(ByVal list As ArrayList, ByVal StartIndex As Long, ByVal Count As Long)
Dim word As Variant
For Each word In list.GetEnumerator(StartIndex, Count)
Console.WriteLine "word is {0}", word
Next word
End Sub
' The following output is produced by this code.
'
' Using For..Each
' The quick brown fox
' Using an Enumerator
' The quick brown fox
' Iterate Subset
' word is quick
' word is brown