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
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"
colCust.Add(New Customer("Ann"
Try
'this does work!
Console.WriteLine(colCust("Joe"
'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