ArrayList: GetEnumerator

GetEnumerator

Returns an enumerator for an ArrayList.



 Public Function GetEnumerator(
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As IEnumerator

Parameters

Index
[ByRef] Optional. Variant. The zero-based index in the list to being enumeration.
Count
[ByRef] Optional. Variant. The number of items in the list to enumerate over.

Return Values

IEnumerator -  An ArrayList enumerator

Remarks

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.

Examples

This example shows several methods of enumerating over an ArrayList object.

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.
    Debug.Print "Using For..Each"
    IterateUsingForEach List
    
    ' Display the words using the enumerator manually.
    Debug.Print "Using an Enumerator"
    IterateUsingEnumerator List
    
    ' Display a subset of the list using For..Each.
    Debug.Print "Itertate Subset"
    IterateSubset List, 1, 2
End Sub

Private Sub IterateUsingForEach(ByVal List As ArrayList)
    Dim Word As Variant
    
    For Each Word In List
        Debug.Print vbTab & Word;
    Next Word
    
    Debug.Print
End Sub

Private Sub IterateUsingEnumerator(ByVal List As ArrayList)
    Dim En As IEnumerator
    
    Set En = List.GetEnumerator
    
    Do While En.MoveNext
        Debug.Print vbTab & En.Current;
    Loop
    
    Debug.Print
End Sub

Private Sub IterateSubset(ByVal List As ArrayList, ByVal StartIndex As Long, ByVal Count As Long)
    Dim Word As Variant
    Dim En As IEnumerator
    
    Set En = List.GetEnumerator(StartIndex, Count)
    
    Do While En.MoveNext
        Debug.Print CorString.Format("{0}word is '{1}'", vbTab, En.Current)
    Loop
End Sub

' The following output is produced by this code.
'
'    Using For..Each
'        The    quick    brown    fox
'    Using an Enumerator
'        The    quick    brown    fox
'    Itertate Subset
'        word is 'quick'
'        word is 'brown'

See Also

Project CorLib Overview

Class ArrayList Overview