Continuation from
Part 1Collections, IEnumerable & IEnumerator (Part 2)What's an Enumerable?An Enumerable is a class that implements the IEnumerable interface
The IEnumerable
CODE
Public Class Example
Implements IEnumerable
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
End Function
End Class
That function returns an Enumemator for the class.
What's an EnumeratorAn Enumerator is a class that implements the IEnumerator Interface. It more detail an Enumerator is a method of iterating through the values of a collection.
CODE
Public Class IEnumerator_Example
Implements IEnumerator
Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
Get
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
End Sub
End Class
So you can see why you the error (shown in Part 1)
Example of a class that uses IEnumerable and IEnumerator
CODE
Public Class MyStrings
Implements IEnumerator, IEnumerable
Private mb As String = {", "B", "C", "D"}
Private i As Integer = -1
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return CType(Me, IEnumerator)
End Function
Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
Get
If i < mb.Count Then
Return mb(i)
Else
Return Nothing
End If
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
If i < mb.Count Then
i += 1
Return True
Else
Return False
End If
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
i = -1
End Sub
End Class
Example Usage
CODE
Module Module1
Sub Main()
Dim bl As New MyStrings
For Each a In bl
Console.WriteLine(a)
Next
End Sub
End Module
Edit: Added link to Part 1.