Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For-Each does not work

Status
Not open for further replies.

andrey

Programmer
Jun 23, 2000
24
IL
Why, if I define some class inherits from DictionaryBase, For-Each does not work? I get the error "Specified cast is not valid". Here is a sample code:

Public Class Customer
Private m_CustName As String
Public ReadOnly Property CustName()
Get
Return m_CustName
End Get
End Property
Public Sub New(ByVal NewName As String)
m_CustName = NewName
End Sub
End Class

Public Class Customers
Inherits DictionaryBase
Public Sub Add(ByVal NewEntry As Customer, ByVal NewKey As String)
Dictionary.Add(NewKey, NewEntry)
End Sub
Default Public ReadOnly Property Item(ByVal Key As String) As Customer
Get
Return CType(Dictionary.Item(Key), Customer)
End Get
End Property
End Class

Module Module1
Sub Main()
Dim colCust As New Customers()
Dim cust As Customer
colCust.Add(New Customer("Joe"), "Joe")
colCust.Add(New Customer("Ann"), "Ann")
Try
'this does work!
Console.WriteLine(colCust("Joe").CustName)
'this does not work!
For Each cust In colCust
Console.WriteLine(cust.CustName)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
Console.ReadLine()
End Sub
End Module

Thank you for advance
 
a good article on creating your own custom collections...


:)
paul
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top