Error in For Each Collection CodeSmith Template

K

Kakaiya

Error message:
An unhandled exception of type 'System.InvalidCastException' occurred
in test.dll
Additional information: Specified cast is not valid.


Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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(oUser.Key, oUser)
oUser = Nothing

oUser = New User
oUser.Key = "BB"
oUser.FirstName = "Bob"
oUser.LastName = "Brown"
oUsers.Add(oUser.Key, oUser)
oUser = Nothing

For Each oUser In oUsers '<-- Error For Each not working?
MsgBox(oUser.Key)
Next
End Sub
End Class


Option Strict On

Imports System
Imports System.Collections

'Error message:
'An unhandled exception of type 'System.InvalidCastException' occurred
in intellisearch.dll
'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 "Constructors"
Public Sub New()
_innerHash = New Hashtable
End Sub

Public Sub New(ByVal original As Users)
_innerHash = New Hashtable(original.InnerHash)
End Sub

Public Sub New(ByVal dictionary As IDictionary)
_innerHash = New Hashtable(dictionary)
End Sub

Public Sub New(ByVal capacity As Integer)
_innerHash = New Hashtable(capacity)
End Sub

Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single)
_innerHash = New Hashtable(dictionary, loadFactor)
End Sub

Public Sub New(ByVal codeProvider As IHashCodeProvider, ByVal
comparer As IComparer)
_innerHash = New Hashtable(codeProvider, comparer)
End Sub

Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer)
_innerHash = New Hashtable(capacity, loadFactor)
End Sub

Public Sub New(ByVal dictionary As IDictionary, ByVal codeProvider
As IHashCodeProvider, ByVal comparer As IComparer)
_innerHash = New Hashtable(dictionary, codeProvider, comparer)
End Sub

Public Sub New(ByVal capacity As Integer, ByVal codeProvider As
IHashCodeProvider, ByVal comparer As IComparer)
_innerHash = New Hashtable(capacity, codeProvider, comparer)
End Sub

Public Sub New(ByVal dictionary As IDictionary, ByVal loadFactor As
Single, ByVal codeProvider As IHashCodeProvider, ByVal comparer As
IComparer)
_innerHash = New Hashtable(dictionary, loadFactor, codeProvider,
comparer)
End Sub

Public Sub New(ByVal capacity As Integer, ByVal loadFactor As
Integer, ByVal codeProvider As IHashCodeProvider, ByVal comparer As
IComparer)
_innerHash = New Hashtable(capacity, loadFactor, codeProvider,
comparer)
End Sub


#End Region

#Region "Implementation of IDictionary"

Private Function _GetEnumerator() As IDictionaryEnumerator
Return New UsersEnumerator(Me)
End Function

Public Function IDictionary_GetEnumerator() As IDictionaryEnumerator
Implements IDictionary.GetEnumerator
Return _GetEnumerator()
End Function

Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
Return _GetEnumerator()
End Function

Public Sub Remove(ByVal key As String)
_innerHash.Remove(key)
End Sub

Public Sub Remove(ByVal key As Object) Implements IDictionary.Remove
Remove(CType(key, String))
End Sub

Public Function Contains(ByVal key As String) As Boolean
Return _innerHash.Contains(key)

End Function

Public Function Contains(ByVal key As Object) As Boolean Implements
IDictionary.Contains
Return Contains(CType(key, String))
End Function

Public Sub Clear() Implements IDictionary.Clear
_innerHash.Clear()
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.IsReadOnly
Get
Return _innerHash.IsReadOnly
End Get
End Property

Default Public Property Item(ByVal key As String) As User
Get
Return CType(_innerHash(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.Item
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.Collections.ICollection
Implements IDictionary.Values
Get
Return _innerHash.Values
End Get
End Property

Public ReadOnly Property Keys() As System.Collections.ICollection
Implements IDictionary.Keys
Get
Return _innerHash.Keys
End Get
End Property

Public ReadOnly Property IsFixedSize() As Boolean Implements
IDictionary.IsFixedSize
Get
Return _innerHash.IsFixedSize
End Get
End Property

#End Region

#Region "Implementation of ICollection"

Public Sub CopyTo(ByVal array As System.Array, ByVal index As
Integer) Implements ICollection.CopyTo
_innerHash.CopyTo(array, index)
End Sub

Public ReadOnly Property IsSynchronized() As Boolean Implements
System.Collections.ICollection.IsSynchronized
Get
Return _innerHash.IsSynchronized
End Get
End Property

Public ReadOnly Property Count() As Integer Implements
System.Collections.ICollection.Count
Get
Return _innerHash.Count
End Get
End Property

Public ReadOnly Property SyncRoot() As Object Implements
System.Collections.ICollection.SyncRoot
Get
Return _innerHash.SyncRoot
End Get
End Property

#End Region

#Region "Implementation of ICloneable"

Public Function Clone() As Users
Dim innerClone As Users = New Users
innerClone._innerHash = CType(_innerHash.Clone(), Hashtable)

Return innerClone
End Function

Public Function ICloneable_Clone() As Object Implements
ICloneable.Clone
Return Clone()
End Function
#End Region

#Region "HashTable Methods"
Public Function ContainsKey(ByVal key As String) As Boolean
Return _innerHash.ContainsKey(key)
End Function

Public Function ContainsValue(ByVal value As User) As Boolean
Return _innerHash.ContainsValue(value)
End Function

Public Shared Function Synchronized(ByVal nonSync As Users) As Users
Dim sync As Users = New Users
sync._innerHash = Hashtable.Synchronized(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 IDictionaryEnumerator

Private innerEnumerator As IDictionaryEnumerator

Friend Sub New(ByVal enumerable As Users)
innerEnumerator = enumerable.InnerHash.GetEnumerator()
End Sub

#Region "Implementation of IDictionaryEnumerator"
Public ReadOnly Property Key() As String
Get
Return CType(innerEnumerator.Key, String)
End Get
End Property

Public ReadOnly Property IDictionaryEnumerator_Key() As Object
Implements IDictionaryEnumerator.Key
Get
Return Key
End Get
End Property

Public ReadOnly Property Value() As User
Get
Return CType(innerEnumerator.Value, User)
End Get
End Property

Public ReadOnly Property IDictionaryEnumerator_Value() As Object
Implements IDictionaryEnumerator.Value
Get
Return Value
End Get
End Property

Public ReadOnly Property Entry() As
System.Collections.DictionaryEntry Implements
IDictionaryEnumerator.Entry
Get
Return innerEnumerator.Entry
End Get
End Property
#End Region

#Region "Implementation of IEnumerator"
Public Sub Reset() Implements IDictionaryEnumerator.Reset
innerEnumerator.Reset()
End Sub

Public Function MoveNext() As Boolean Implements
IDictionaryEnumerator.MoveNext
Return innerEnumerator.MoveNext()
End Function

Public ReadOnly Property Current() As Object Implements
IDictionaryEnumerator.Current
Get
Return innerEnumerator.Current
End Get
End Property
#End Region
End Class
'******** Users Collection Class *************
 
J

Jon Skeet [C# MVP]

Kakaiya said:
Error message:
An unhandled exception of type 'System.InvalidCastException' occurred
in test.dll
Additional information: Specified cast is not valid.

Yup - because your enumerator is enumerating DictionaryEntry objects,
not Users, as far as I can tell.
 
K

Kakaiya

Error message:
An unhandled exception of type 'System.InvalidCastException' occurred
in test.dll
Additional information: Specified cast is not valid.

How can I do below by updating UsersEnumerator class?
For Each oUser In oUsers
MsgBox(oUser.Key)
Next

and How can I retrieve User class based on Index?

************************************
 
J

Jon Skeet [C# MVP]

Kakaiya said:
Error message:
An unhandled exception of type 'System.InvalidCastException' occurred
in test.dll
Additional information: Specified cast is not valid.

How can I do below by updating UsersEnumerator class?
For Each oUser In oUsers
MsgBox(oUser.Key)
Next

Try changing it to

For Each oUser in oUsers.Values
and How can I retrieve User class based on Index?

Do you mean by key? Hashtables aren't ordered, so there's no real
concept of index.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top