IEnumerable

IEnumerable


An interface that can be used to get an enumerator for an object.


Public:

Methods:

NameDescription
 GetEnumerator Returns an enumerator that iterates through a collection.  

Remarks

Implement this interface for general cases where only an enumerator is needed.

Examples

The following example demonstrates an implementation of the IEnumerable and IEnumerator interfaces by creating a simple container class with an associated enumerator.

The example consists of three files. Two of the files are classes named Container and ContainerEnumerator. There is also one BAS file that contains the Main method.


The Container class implementation.
Option Explicit
Implements IEnumerable

Private mItems() As Variant
Private mCount As Long


Public Property Get Count() As Long
    Count = mCount
End Property

Public Sub Add(ByRef Value As Variant)
    If mCount > UBound(mItems) Then
        ReDim Preserve mItems(0 To UBound(mItems) * 2)
    End If
    
    CopyVariant mItems(mCount), Value
    mCount = mCount + 1
End Sub

Public Property Get Item(ByVal Index As Long) As Variant
    CopyVariant Item, mItems(Index)
End Property

Public Function GetEnumerator() As IEnumerator
    Dim En As New ContainerEnumerator
    
    En.Init Me
    Set GetEnumerator = En
End Function

Public Function NewEnum() As IUnknown
    Set NewEnum = CreateEnumerator(GetEnumerator)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Constructors
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
    ReDim mItems(4)
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   IEnumerable
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IEnumerable_GetEnumerator() As CorLib.IEnumerator
    Set IEnumerable_GetEnumerator = GetEnumerator
End Function

Private Function IEnumerable_NewEnum() As stdole.IUnknown
    Set IEnumerable_NewEnum = NewEnum
End Function

The ContainerEnumerator class implementation.
Option Explicit
Implements IEnumerator

Private mBase As EnumeratorBase
Private mContainer As Container


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Constructors
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub Init(ByVal Container As Container)
    Set mBase = NewEnumeratorBase(0, Container.Count)
    Set mContainer = Container
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   IEnumerator
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Property Get IEnumerator_Current() As Variant
    MoveVariant IEnumerator_Current, mContainer.Item(mBase.Index)
End Property

Private Function IEnumerator_MoveNext() As Boolean
    IEnumerator_MoveNext = mBase.MoveNext
End Function

Private Sub IEnumerator_Reset()
    mBase.Reset
End Sub

The Main method implemented in a BAS module.
Public Sub Main()
    Dim Items As New Container
    
    Items.Add "The"
    Items.Add "fox"
    Items.Add "jumps"
    Items.Add "over"
    Items.Add "the"
    Items.Add "lazy"
    Items.Add "dog."
    
    Debug.Print "Enumerate using 'For Each'."
    EnumerateWithForEach Items
    
    Debug.Print t("\nEnumerate using 'IEnumerator' interface.")
    EnumerateWithEnumerator Items
End Sub

Private Sub EnumerateWithForEach(ByVal Source As IEnumerable)
    Dim Value As Variant
    
    Debug.Print "   ";
    
    For Each Value In Source
        Debug.Print Value & " ";
    Next
    
    Debug.Print
End Sub

Private Sub EnumerateWithEnumerator(ByVal Source As IEnumerable)
    Dim En As IEnumerator
    
    Debug.Print "   ";
    
    Set En = Source.GetEnumerator
    
    Do While En.MoveNext
        Debug.Print En.Current & " ";
    Loop
    
    Debug.Print
End Sub

' The example code produces the following output.
'
'    Enumerate using 'For Each'.
'       The fox jumps over the lazy dog.
'
'    Enumerate using 'IEnumerator' interface.
'       The fox jumps over the lazy dog.

See Also

Project CorLib Overview

Class IEnumerable Overview