Implementing the IEnumerator interface in my custom class 
Author Message
 Implementing the IEnumerator interface in my custom class

Hi there,

I made a custom class to enumerate through the category rows in the Northwind database and returning facade objects for a category instead of the actual datarow.
(See code below)
I've implemented the IEnumerator interface in my class according to the sample in the MSDN library.

The IEnumerator.Reset method is implemented as just resetting the EnumeratorPosition to -1.

When I loop through the items in my custom collection, this works fine for my first loop.

But when I try to loop for a second time... this no longer works.
As it seems, the Reset method is not called (automatically)?!

Who should call this method?!
Do I have to implement this in my custom class (for example in the MoveNext method when you notice that the end has been reached)... or should the framework do this when the For Each loop is exited or ended?!

Best regards,
Philippe Rubbrecht.

Public Class Collection
    Implements IEnumerable, IEnumerator
    Private mdtbItems As DataTable
    Private EnumeratorPosition As Integer = -1

    Public Sub New()
        Dim cnn As OleDbConnection = New OleDbConnection("File Name=C:\Northwind.udl")
        Dim dap As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Categories", cnn)
        Dim dst As DataSet = New DataSet
        Try
            dap.Fill(dst, "Categories")
            mdtbItems = dst.Tables("Categories")
         Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            cnn.Close()
        End Try
    End Sub

    Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return CType(Me, IEnumerator)
    End Function

    Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
        Get
            Return New Item(mdtbItems.Rows(EnumeratorPosition))
        End Get
    End Property

    Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
        EnumeratorPosition += 1
        Do While EnumeratorPosition < mdtbItems.Rows.Count AndAlso mdtbItems.Rows(EnumeratorPosition).RowState = DataRowState.Deleted
            EnumeratorPosition += 1
        Loop
        Dim blnEndOfCollection As Boolean = (EnumeratorPosition < mdtbItems.Rows.Count)
        Return Not blnEndOfCollection
    End Function

    Public Sub Reset() Implements System.Collections.IEnumerator.Reset
        EnumeratorPosition = -1
    End Sub
End Class

Public Class Item
    Private mdrwItem As DataRow

    Friend Sub New(ByRef DataRow As DataRow)
        mdrwItem = DataRow
    End Sub

    Public ReadOnly Property CategoryName() As String
        Get
            Return mdrwItem.Item("CategoryName").ToString
        End Get
    End Property

    Public Sub Delete()
        mdrwItem.Delete()
    End Sub
End Class



Tue, 22 Nov 2005 20:15:29 GMT  
 Implementing the IEnumerator interface in my custom class
Rubbrecht,
Generally you should have a separate class that implements IEnumerator, most
times I will make it a nested class.

Then in the GetEnumerator method, return a new enumerator object.

Something like:

Public Class Collection
    Implements IEnumerable

    ' nested class
    Private Class CollectionEnumerator
        ' actual implementation below
    End Class

    ...

    Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
        Return New CollectionEnumerator(mdtbItems)
    End Function

    ...
End Class

' this goes above
Private Class CollectionEnumerator
    Implements IEnumerator

    Private mdtbItems As DataTable
    Private EnumeratorPosition As Integer = -1

    Public Sub New(ByVal items as DataTable)
        mdtbItems = items
    End Sub

    Public ReadOnly Property Current() As Object Implements
IEnumerator.Current
        Get
            Return New Item(mdtbItems.Rows(EnumeratorPosition))
        End Get
    End Property

    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
        EnumeratorPosition += 1
        Do While EnumeratorPosition < mdtbItems.Rows.Count AndAlso
mdtbItems.Rows(EnumeratorPosition).RowState = DataRowState.Deleted
            EnumeratorPosition += 1
        Loop
        Dim blnEndOfCollection As Boolean = (EnumeratorPosition <
mdtbItems.Rows.Count)
        Return Not blnEndOfCollection
    End Function

    Public Sub Reset() Implements IEnumerator.Reset
        EnumeratorPosition = -1
    End Sub

End Class

I make the class nested for encapsulation reasons, CollectionEnumerator does
not make sense outside the implementation of Collection.

Hope this helps
Jay



Hi there,

I made a custom class to enumerate through the category rows in the
Northwind database and returning facade objects for a category instead of
the actual datarow.
(See code below)
I've implemented the IEnumerator interface in my class according to the
sample in the MSDN library.

The IEnumerator.Reset method is implemented as just resetting the
EnumeratorPosition to -1.

When I loop through the items in my custom collection, this works fine for
my first loop.

But when I try to loop for a second time... this no longer works.
As it seems, the Reset method is not called (automatically)?!

Who should call this method?!
Do I have to implement this in my custom class (for example in the MoveNext
method when you notice that the end has been reached)... or should the
framework do this when the For Each loop is exited or ended?!

Best regards,
Philippe Rubbrecht.

Public Class Collection
    Implements IEnumerable, IEnumerator
    Private mdtbItems As DataTable
    Private EnumeratorPosition As Integer = -1

    Public Sub New()
        Dim cnn As OleDbConnection = New OleDbConnection("File
Name=C:\Northwind.udl")
        Dim dap As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM
Categories", cnn)
        Dim dst As DataSet = New DataSet
        Try
            dap.Fill(dst, "Categories")
            mdtbItems = dst.Tables("Categories")
         Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            cnn.Close()
        End Try
    End Sub

    Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
        Return CType(Me, IEnumerator)
    End Function

    Public ReadOnly Property Current() As Object Implements
System.Collections.IEnumerator.Current
        Get
            Return New Item(mdtbItems.Rows(EnumeratorPosition))
        End Get
    End Property

    Public Function MoveNext() As Boolean Implements
System.Collections.IEnumerator.MoveNext
        EnumeratorPosition += 1
        Do While EnumeratorPosition < mdtbItems.Rows.Count AndAlso
mdtbItems.Rows(EnumeratorPosition).RowState = DataRowState.Deleted
            EnumeratorPosition += 1
        Loop
        Dim blnEndOfCollection As Boolean = (EnumeratorPosition <
mdtbItems.Rows.Count)
        Return Not blnEndOfCollection
    End Function

    Public Sub Reset() Implements System.Collections.IEnumerator.Reset
        EnumeratorPosition = -1
    End Sub
End Class

Public Class Item
    Private mdrwItem As DataRow

    Friend Sub New(ByRef DataRow As DataRow)
        mdrwItem = DataRow
    End Sub

    Public ReadOnly Property CategoryName() As String
        Get
            Return mdrwItem.Item("CategoryName").ToString
        End Get
    End Property

    Public Sub Delete()
        mdrwItem.Delete()
    End Sub
End Class



Wed, 23 Nov 2005 00:09:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reset() when implementing IEnumerator

2. How to Implement a Custom Interface?

3. Implement custom interface

4. How To Use Implemented Interface with RDS and Custom Business Object

5. Implementing IUnknown interface in Access 2000 class

6. Which classes implement an interface?

7. Need help Implement a class interface.

8. Identifying classes/components that implements certain interfaces

9. Implementing public interface of another class

10. IMPLEMENTING AN INTERFACE MADE FROM ANOTHER INTERFACE

11. Implementing an interface made from another interface

12. IMPLEMENTING AN INTERFACE MADE FROM ANOTHER INTERFACE

 

 
Powered by phpBB® Forum Software