IEnumerable Interface in Visual Basic .NET

Last Updated: March 19, 2020

To understand the IEnumerable interface in VB.NET, I will create Student class and Course class. Since course has students I can use For...Each loop to get students from the course

First I will create the Student class. It has the FirstName and LastName property

Public Class Student
    Private m_firstName As String
    Private m_lastName As String

    Public Property FirstName() As String
        Get
            Return m_firstName
        End Get
        Set(value As String)
            m_firstName = value
        End Set
    End Property


    Public Property LastName() As String
        Get
            Return m_lastName
        End Get
        Set(value As String)
            m_lastName = value
        End Set
    End Property
End Class

Then I will create the Course class which implements the IEnumerable interface

Public Class Course
    Implements Collections.IEnumerable
    Private students() As Student

    Public Sub New(ByVal students() As Student)
        Me.students = students

    End Sub
    Function GetEnumerator() As IEnumerator Implements Collections.IEnumerable.GetEnumerator
        Return New StudentsEnum(students)
    End Function
End Class



Code explanation

  • Line 9: You can see the GetEnumerator() function which return the IEnumerator type

Now I am going to create StudentEnum class which implements the IEnumerator

Public Class StudentsEnum
    Implements IEnumerator

    Dim position As Integer = -1
    Dim students() As Student

    Public Sub New(ByVal students() As Student)
        Me.students = students
    End Sub
    Public Sub Reset() Implements IEnumerator.Reset
        position = -1
    End Sub


    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext

        position = position + 1
        If (position >= students.Length) Then
            Return False
        Else
            Return position < students.Length
        End If

    End Function

    Public ReadOnly Property Current() As Object Implements IEnumerator.Current
        Get
            Return (students(position))
        End Get
    End Property


End Class

In this class you have to implement Reset(), MoveNext(), Current()

Code explanation

  • Line   4: Position variable is used as the index of an array
  • Line   5: students() array to hold students
  • Line 10: Reset() function is used to reset the index position
  • Line 15: MoveNext() function is used to increase the position
  • Line 26: Current() function is used get the current object when you iterate

Now we can write the code to create some students objects and to the Course constructor method. Finally you can do the iterations

Module Module1

    Sub Main()

        'Create two students
        Dim students(1) As Student
        Dim s1 As New Student()
        s1.FirstName = "John"
        s1.LastName = "Doe"
        students(0) = s1

        Dim s2 As New Student()
        s2.FirstName = "Mark"
        s2.LastName = "Taylor"
        students(1) = s2

        'Create VBCouse with Students
        Dim VBCourse As New Course(students)

        'Now iterate to get students

        For Each stu As Student In VBCourse
            Console.WriteLine(stu.LastName)
        Next

        Console.ReadLine()

    End Sub

End Module