Error message:
An unhandled exception of type 'System.Invalid CastException' occurred
in test.dll
Additional information: Specified cast is not valid.
Public Class Form1
Inherits System.Windows. Forms.Form
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim oUsers As New Users
Dim oUser As User
oUser = New User
oUser.Key = "JJ"
oUser.FirstName = "Joe"
oUser.LastName = "Jason"
oUsers.Add(oUse r.Key, oUser)
oUser = Nothing
oUser = New User
oUser.Key = "BB"
oUser.FirstName = "Bob"
oUser.LastName = "Brown"
oUsers.Add(oUse r.Key, oUser)
oUser = Nothing
For Each oUser In oUsers '<-- Error For Each not working?
MsgBox(oUser.Ke y)
Next
End Sub
End Class
Option Strict On
Imports System
Imports System.Collecti ons
'Error message:
'An unhandled exception of type 'System.Invalid CastException' occurred
in intellisearch.d ll
'Additional information: Specified cast is not valid.
'******** User Class *************
Public Class User
Dim m_Key As String
Dim m_FirstName As String
Dim m_LastName As String
Public Property Key() As String
Get
Return m_Key
End Get
Set(ByVal Value As String)
m_Key = Value
End Set
End Property
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(ByVal Value As String)
m_FirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(ByVal Value As String)
m_LastName = Value
End Set
End Property
End Class
'******** User Class *************
'******** Users Collection Class *************
Public Class Users
Implements IDictionary
Implements ICollection
Implements IEnumerable
Implements ICloneable
Protected _innerHash As Hashtable
#Region "Constructo rs"
Public Sub New()
_innerHash = New Hashtable
End Sub
Public Sub New(ByVal original As Users)
_innerHash = New Hashtable(origi nal.InnerHash)
End Sub
Public Sub New(ByVal dictionary As IDictionary)
_innerHash = New Hashtable(dicti onary)
End Sub
Public Sub New(ByVal capacity As Integer)
_innerHash = New Hashtable(capac ity)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single)
_innerHash = New Hashtable(dicti onary, loadFactor)
End Sub
Public Sub New(ByVal codeProvider As IHashCodeProvid er, ByVal
comparer As IComparer)
_innerHash = New Hashtable(codeP rovider, comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer)
_innerHash = New Hashtable(capac ity, loadFactor)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal codeProvider
As IHashCodeProvid er, ByVal comparer As IComparer)
_innerHash = New Hashtable(dicti onary, codeProvider, comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal codeProvider As
IHashCodeProvid er, ByVal comparer As IComparer)
_innerHash = New Hashtable(capac ity, codeProvider, comparer)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single, ByVal codeProvider As IHashCodeProvid er, ByVal comparer As
IComparer)
_innerHash = New Hashtable(dicti onary, loadFactor, codeProvider,
comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer, ByVal codeProvider As IHashCodeProvid er, ByVal comparer As
IComparer)
_innerHash = New Hashtable(capac ity, loadFactor, codeProvider,
comparer)
End Sub
#End Region
#Region "Implementa tion of IDictionary"
Private Function _GetEnumerator( ) As IDictionaryEnum erator
Return New UsersEnumerator (Me)
End Function
Public Function IDictionary_Get Enumerator() As IDictionaryEnum erator
Implements IDictionary.Get Enumerator
Return _GetEnumerator( )
End Function
Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.Get Enumerator
Return _GetEnumerator( )
End Function
Public Sub Remove(ByVal key As String)
_innerHash.Remo ve(key)
End Sub
Public Sub Remove(ByVal key As Object) Implements IDictionary.Rem ove
Remove(CType(ke y, String))
End Sub
Public Function Contains(ByVal key As String) As Boolean
Return _innerHash.Cont ains(key)
End Function
Public Function Contains(ByVal key As Object) As Boolean Implements
IDictionary.Con tains
Return Contains(CType( key, String))
End Function
Public Sub Clear() Implements IDictionary.Cle ar
_innerHash.Clea r()
End Sub
Public Sub Add(ByVal key As String, ByVal value As User)
_innerHash.Add( key, value)
End Sub
Public Sub Add(ByVal key As Object, ByVal value As Object)
Implements IDictionary.Add
Add(CType(key, String), CType(value, User))
End Sub
Public ReadOnly Property IsReadOnly() As Boolean Implements
IDictionary.IsR eadOnly
Get
Return _innerHash.IsRe adOnly
End Get
End Property
Default Public Property Item(ByVal key As String) As User
Get
Return CType(_innerHas h(key), User)
End Get
Set(ByVal Value As User)
_innerHash(key) = Value
End Set
End Property
Default Public Property Item(ByVal key As Object) As Object
Implements IDictionary.Ite m
Get
Return Item(CType(key, String))
End Get
Set(ByVal Value As Object)
Item(CType(key, String)) = CType(Value, User)
End Set
End Property
Public ReadOnly Property Values() As System.Collecti ons.ICollection
Implements IDictionary.Val ues
Get
Return _innerHash.Valu es
End Get
End Property
Public ReadOnly Property Keys() As System.Collecti ons.ICollection
Implements IDictionary.Key s
Get
Return _innerHash.Keys
End Get
End Property
Public ReadOnly Property IsFixedSize() As Boolean Implements
IDictionary.IsF ixedSize
Get
Return _innerHash.IsFi xedSize
End Get
End Property
#End Region
#Region "Implementa tion of ICollection"
Public Sub CopyTo(ByVal array As System.Array, ByVal index As
Integer) Implements ICollection.Cop yTo
_innerHash.Copy To(array, index)
End Sub
Public ReadOnly Property IsSynchronized( ) As Boolean Implements
System.Collecti ons.ICollection .IsSynchronized
Get
Return _innerHash.IsSy nchronized
End Get
End Property
Public ReadOnly Property Count() As Integer Implements
System.Collecti ons.ICollection .Count
Get
Return _innerHash.Coun t
End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements
System.Collecti ons.ICollection .SyncRoot
Get
Return _innerHash.Sync Root
End Get
End Property
#End Region
#Region "Implementa tion of ICloneable"
Public Function Clone() As Users
Dim innerClone As Users = New Users
innerClone._inn erHash = CType(_innerHas h.Clone(), Hashtable)
Return innerClone
End Function
Public Function ICloneable_Clon e() As Object Implements
ICloneable.Clon e
Return Clone()
End Function
#End Region
#Region "HashTable Methods"
Public Function ContainsKey(ByV al key As String) As Boolean
Return _innerHash.Cont ainsKey(key)
End Function
Public Function ContainsValue(B yVal value As User) As Boolean
Return _innerHash.Cont ainsValue(value )
End Function
Public Shared Function Synchronized(By Val nonSync As Users) As Users
Dim sync As Users = New Users
sync._innerHash = Hashtable.Synch ronized(nonSync .InnerHash)
Return sync
End Function
#End Region
Friend ReadOnly Property InnerHash() As Hashtable
Get
Return _innerHash
End Get
End Property
End Class
Public Class UsersEnumerator
Implements IDictionaryEnum erator
Private innerEnumerator As IDictionaryEnum erator
Friend Sub New(ByVal enumerable As Users)
innerEnumerator = enumerable.Inne rHash.GetEnumer ator()
End Sub
#Region "Implementa tion of IDictionaryEnum erator"
Public ReadOnly Property Key() As String
Get
Return CType(innerEnum erator.Key, String)
End Get
End Property
Public ReadOnly Property IDictionaryEnum erator_Key() As Object
Implements IDictionaryEnum erator.Key
Get
Return Key
End Get
End Property
Public ReadOnly Property Value() As User
Get
Return CType(innerEnum erator.Value, User)
End Get
End Property
Public ReadOnly Property IDictionaryEnum erator_Value() As Object
Implements IDictionaryEnum erator.Value
Get
Return Value
End Get
End Property
Public ReadOnly Property Entry() As
System.Collecti ons.DictionaryE ntry Implements
IDictionaryEnum erator.Entry
Get
Return innerEnumerator .Entry
End Get
End Property
#End Region
#Region "Implementa tion of IEnumerator"
Public Sub Reset() Implements IDictionaryEnum erator.Reset
innerEnumerator .Reset()
End Sub
Public Function MoveNext() As Boolean Implements
IDictionaryEnum erator.MoveNext
Return innerEnumerator .MoveNext()
End Function
Public ReadOnly Property Current() As Object Implements
IDictionaryEnum erator.Current
Get
Return innerEnumerator .Current
End Get
End Property
#End Region
End Class
'******** Users Collection Class *************
An unhandled exception of type 'System.Invalid CastException' occurred
in test.dll
Additional information: Specified cast is not valid.
Public Class Form1
Inherits System.Windows. Forms.Form
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim oUsers As New Users
Dim oUser As User
oUser = New User
oUser.Key = "JJ"
oUser.FirstName = "Joe"
oUser.LastName = "Jason"
oUsers.Add(oUse r.Key, oUser)
oUser = Nothing
oUser = New User
oUser.Key = "BB"
oUser.FirstName = "Bob"
oUser.LastName = "Brown"
oUsers.Add(oUse r.Key, oUser)
oUser = Nothing
For Each oUser In oUsers '<-- Error For Each not working?
MsgBox(oUser.Ke y)
Next
End Sub
End Class
Option Strict On
Imports System
Imports System.Collecti ons
'Error message:
'An unhandled exception of type 'System.Invalid CastException' occurred
in intellisearch.d ll
'Additional information: Specified cast is not valid.
'******** User Class *************
Public Class User
Dim m_Key As String
Dim m_FirstName As String
Dim m_LastName As String
Public Property Key() As String
Get
Return m_Key
End Get
Set(ByVal Value As String)
m_Key = Value
End Set
End Property
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(ByVal Value As String)
m_FirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(ByVal Value As String)
m_LastName = Value
End Set
End Property
End Class
'******** User Class *************
'******** Users Collection Class *************
Public Class Users
Implements IDictionary
Implements ICollection
Implements IEnumerable
Implements ICloneable
Protected _innerHash As Hashtable
#Region "Constructo rs"
Public Sub New()
_innerHash = New Hashtable
End Sub
Public Sub New(ByVal original As Users)
_innerHash = New Hashtable(origi nal.InnerHash)
End Sub
Public Sub New(ByVal dictionary As IDictionary)
_innerHash = New Hashtable(dicti onary)
End Sub
Public Sub New(ByVal capacity As Integer)
_innerHash = New Hashtable(capac ity)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single)
_innerHash = New Hashtable(dicti onary, loadFactor)
End Sub
Public Sub New(ByVal codeProvider As IHashCodeProvid er, ByVal
comparer As IComparer)
_innerHash = New Hashtable(codeP rovider, comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer)
_innerHash = New Hashtable(capac ity, loadFactor)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal codeProvider
As IHashCodeProvid er, ByVal comparer As IComparer)
_innerHash = New Hashtable(dicti onary, codeProvider, comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal codeProvider As
IHashCodeProvid er, ByVal comparer As IComparer)
_innerHash = New Hashtable(capac ity, codeProvider, comparer)
End Sub
Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single, ByVal codeProvider As IHashCodeProvid er, ByVal comparer As
IComparer)
_innerHash = New Hashtable(dicti onary, loadFactor, codeProvider,
comparer)
End Sub
Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer, ByVal codeProvider As IHashCodeProvid er, ByVal comparer As
IComparer)
_innerHash = New Hashtable(capac ity, loadFactor, codeProvider,
comparer)
End Sub
#End Region
#Region "Implementa tion of IDictionary"
Private Function _GetEnumerator( ) As IDictionaryEnum erator
Return New UsersEnumerator (Me)
End Function
Public Function IDictionary_Get Enumerator() As IDictionaryEnum erator
Implements IDictionary.Get Enumerator
Return _GetEnumerator( )
End Function
Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.Get Enumerator
Return _GetEnumerator( )
End Function
Public Sub Remove(ByVal key As String)
_innerHash.Remo ve(key)
End Sub
Public Sub Remove(ByVal key As Object) Implements IDictionary.Rem ove
Remove(CType(ke y, String))
End Sub
Public Function Contains(ByVal key As String) As Boolean
Return _innerHash.Cont ains(key)
End Function
Public Function Contains(ByVal key As Object) As Boolean Implements
IDictionary.Con tains
Return Contains(CType( key, String))
End Function
Public Sub Clear() Implements IDictionary.Cle ar
_innerHash.Clea r()
End Sub
Public Sub Add(ByVal key As String, ByVal value As User)
_innerHash.Add( key, value)
End Sub
Public Sub Add(ByVal key As Object, ByVal value As Object)
Implements IDictionary.Add
Add(CType(key, String), CType(value, User))
End Sub
Public ReadOnly Property IsReadOnly() As Boolean Implements
IDictionary.IsR eadOnly
Get
Return _innerHash.IsRe adOnly
End Get
End Property
Default Public Property Item(ByVal key As String) As User
Get
Return CType(_innerHas h(key), User)
End Get
Set(ByVal Value As User)
_innerHash(key) = Value
End Set
End Property
Default Public Property Item(ByVal key As Object) As Object
Implements IDictionary.Ite m
Get
Return Item(CType(key, String))
End Get
Set(ByVal Value As Object)
Item(CType(key, String)) = CType(Value, User)
End Set
End Property
Public ReadOnly Property Values() As System.Collecti ons.ICollection
Implements IDictionary.Val ues
Get
Return _innerHash.Valu es
End Get
End Property
Public ReadOnly Property Keys() As System.Collecti ons.ICollection
Implements IDictionary.Key s
Get
Return _innerHash.Keys
End Get
End Property
Public ReadOnly Property IsFixedSize() As Boolean Implements
IDictionary.IsF ixedSize
Get
Return _innerHash.IsFi xedSize
End Get
End Property
#End Region
#Region "Implementa tion of ICollection"
Public Sub CopyTo(ByVal array As System.Array, ByVal index As
Integer) Implements ICollection.Cop yTo
_innerHash.Copy To(array, index)
End Sub
Public ReadOnly Property IsSynchronized( ) As Boolean Implements
System.Collecti ons.ICollection .IsSynchronized
Get
Return _innerHash.IsSy nchronized
End Get
End Property
Public ReadOnly Property Count() As Integer Implements
System.Collecti ons.ICollection .Count
Get
Return _innerHash.Coun t
End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements
System.Collecti ons.ICollection .SyncRoot
Get
Return _innerHash.Sync Root
End Get
End Property
#End Region
#Region "Implementa tion of ICloneable"
Public Function Clone() As Users
Dim innerClone As Users = New Users
innerClone._inn erHash = CType(_innerHas h.Clone(), Hashtable)
Return innerClone
End Function
Public Function ICloneable_Clon e() As Object Implements
ICloneable.Clon e
Return Clone()
End Function
#End Region
#Region "HashTable Methods"
Public Function ContainsKey(ByV al key As String) As Boolean
Return _innerHash.Cont ainsKey(key)
End Function
Public Function ContainsValue(B yVal value As User) As Boolean
Return _innerHash.Cont ainsValue(value )
End Function
Public Shared Function Synchronized(By Val nonSync As Users) As Users
Dim sync As Users = New Users
sync._innerHash = Hashtable.Synch ronized(nonSync .InnerHash)
Return sync
End Function
#End Region
Friend ReadOnly Property InnerHash() As Hashtable
Get
Return _innerHash
End Get
End Property
End Class
Public Class UsersEnumerator
Implements IDictionaryEnum erator
Private innerEnumerator As IDictionaryEnum erator
Friend Sub New(ByVal enumerable As Users)
innerEnumerator = enumerable.Inne rHash.GetEnumer ator()
End Sub
#Region "Implementa tion of IDictionaryEnum erator"
Public ReadOnly Property Key() As String
Get
Return CType(innerEnum erator.Key, String)
End Get
End Property
Public ReadOnly Property IDictionaryEnum erator_Key() As Object
Implements IDictionaryEnum erator.Key
Get
Return Key
End Get
End Property
Public ReadOnly Property Value() As User
Get
Return CType(innerEnum erator.Value, User)
End Get
End Property
Public ReadOnly Property IDictionaryEnum erator_Value() As Object
Implements IDictionaryEnum erator.Value
Get
Return Value
End Get
End Property
Public ReadOnly Property Entry() As
System.Collecti ons.DictionaryE ntry Implements
IDictionaryEnum erator.Entry
Get
Return innerEnumerator .Entry
End Get
End Property
#End Region
#Region "Implementa tion of IEnumerator"
Public Sub Reset() Implements IDictionaryEnum erator.Reset
innerEnumerator .Reset()
End Sub
Public Function MoveNext() As Boolean Implements
IDictionaryEnum erator.MoveNext
Return innerEnumerator .MoveNext()
End Function
Public ReadOnly Property Current() As Object Implements
IDictionaryEnum erator.Current
Get
Return innerEnumerator .Current
End Get
End Property
#End Region
End Class
'******** Users Collection Class *************
Comment