By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

IList (of Type) Enumerator Error

P: n/a
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).Applicat ionName)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Jan 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Paul wrote:
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.

--
Tom Shelton

Jan 16 '07 #2

P: n/a
Thanks Tom. That worked.

Jan 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.