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
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