EnumeratorBase |
ICloneable |
Name | Description |
---|---|
Index (get) | Gets the current index. |
Name | Description |
---|---|
Clone | Creates a new instance of the enumerator base at the current index position. |
MoveNext | Advances the current index, returning if the advancement succeeded. |
Reset | Resets the index back to the initial starting position. |
Use this class to handle the starting, advancing and finishing of an index.
The class itself does not access a list of items. Instead, it manages an index into a list of items that are being enumerated. It deals with ensuring the enumeration starts correctly and when the enumeration has finished.
To create a new instance use the NewEnumeratorBase method.
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.
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
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
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.