IList (of Type) Enumerator Error

P

Paul

I have two classes. One (FrontierApplication) is a definition of a
record from a table. The other (FrontierApplicationCollection) is the
collection of the first class. Below is how they are defined. I get
an error when iterating through the collection. The error I get is
"Unable to cast object of type 'ArrayListEnumeratorSimple' to type
'System.Collections.Generic.IEnumerator'" The error occurs in the
collection class from the GetEnumerator function. When I try to use
the "For Each . . . " below it generates the error. When I use the
"For i as integer = 0 to . . . " it works correctly. I want to use the
"For Each". Why do I get this error?

Here is the code:

Public Class FrontierApplication

... generic stuff with properties

End Class

Public Class FrontierApplicationCollection
Implements IList(Of FrontierApplication)

Private myArrayList As New ArrayList

... generic IList properties, subs and function

Public Function GetEnumerator() As
System.Collections.Generic.IEnumerator(Of FrontierApplication)
Implements System.Collections.Generic.IEnumerable(Of
FrontierApplication).GetEnumerator
Return myArrayList.GetEnumerator() ' Error occurs here
End Function

End Class

Private myFrontierApplications As New FrontierApplicationCollection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
'
' This generates the error.
'
'For Each f As FrontierApplication In
myFrontierApplications
' Debug.WriteLine(f.ApplicationName)
'Next
'
' This works just fine.
'
For i As Integer = 0 To myFrontierApplications.Count - 1

Debug.WriteLine(myFrontierApplications(i).ApplicationName)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
 
T

Tom Shelton

Paul said:
I have two classes. One (FrontierApplication) is a definition of a
record from a table. The other (FrontierApplicationCollection) is the
collection of the first class. Below is how they are defined. I get
an error when iterating through the collection. The error I get is
"Unable to cast object of type 'ArrayListEnumeratorSimple' to type
'System.Collections.Generic.IEnumerator'" The error occurs in the
collection class from the GetEnumerator function. When I try to use
the "For Each . . . " below it generates the error. When I use the
"For i as integer = 0 to . . . " it works correctly. I want to use the
"For Each". Why do I get this error?

Here is the code:

Public Class FrontierApplication

... generic stuff with properties

End Class

Public Class FrontierApplicationCollection
Implements IList(Of FrontierApplication)

Private myArrayList As New ArrayList

The array list is yur problem... Change it to a List from
System.Collections.Generic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top