Advanced Hashtable ?

Question

Hi,

Is there a way when using a hashtable in VB.NET to add value of a
specific type and retrieve that same value without having to cast it to
the original type?

Thanks
Marty

Solution
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub ''Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function ''Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub ''Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711

on1.401@clgrps13...
Hi,

Is there a way when using a hashtable in VB.NET to add value of a specific
type and retrieve that same value without having to cast it to the
original type?

Thanks
Marty


Hi Robin,

Thanks for the information. Does MSDN has details about the performance
of using this algorithm? Because it still has to do a cast before
returning the value.

Thank you,
Marty

Robin Tucker wrote:
Yes, you can derive from Dictionary and the override the methods you want to
use, performing the cast. This is from MSDN, and shows an example of using
a string type instead of generic object.
Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(key As [String]) As [String]
Get
Return CType(Dictionary(key), [String])
End Get
Set
Dictionary(key) = value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(key As [String], value As [String])
Dictionary.Add(key, value)
End Sub ''Add

Public Function Contains(key As [String]) As Boolean
Return Dictionary.Contains(key)
End Function ''Contains

Public Sub Remove(key As [String])
Dictionary.Remove(key)
End Sub ''Remove

End Class

"Marty" <xm******@hotmail.com> wrote in message
news:H3jne.28711