VB.NET: Improved multidimensional hashtable
09 Jul 2007
in the early morning
Matt Winckler
cheap cialis pill
certified cialis
cheap viagra in canada
cialis buy drug
buy generic cialis
viagra buy
25mg viagra
cheap viagra without prescription
buy cheapest viagra on line
purchase viagra
cialis 10mg
buying generic viagra
cialis pills
viagra from india
cheapest sildenafil citrate
cheap cialis no rx
viagra india
cialis bangkok
viagra for order
buy sildenafil internet
buy generic viagra online
buying cialis online
where to order cialis
tablet cialis
find cialis no prescription required
viagra cheap drug
order cialis cheap online
online pharmacy cialis
cialis no rx
order generic cialis
price of cialis
viagra soft
drug viagra
cheap viagra from uk
order cialis no prescription
order cheap viagra
viagra drug
order cheap cialis
cheap cialis pharmacy
best price for viagra
cheap viagra from usa
cost cialis
cialis overnight shipping
cheapest generic cialis online
generic viagra online
online viagra
viagra sales
cheap cialis in canada
compare cialis prices online
cialis online
drug viagra online purchase
discount cialis without prescription
no rx viagra
cialis overnight
viagra uk
cialis order
cheap cialis from usa
buying cialis
cialis overnight delivery
cialis in bangkok
buy and purchase sildenafil online
impotence treatment
cheap price viagra
viagra sale
cheap cialis tablet
drug cialis
generic cialis online
cheap viagra pharmacy
find discount cialis online
viagra malaysia
cialis without a prescription
buy cialis online cheap
viagra rx
buy no rx viagra
cialis 20mg
viagra in malaysia
discount viagra online
buy sildenafil cheap
buy viagra low price
buy cialis
cialis
cheap price cialis
cheap generic viagra
cialis canada
low cost viagra
buy cheap viagra
cialis vs viagra
order cialis from us
cialis tablets
find no rx cialis
buy generic cialis online
buy viagra overnight delivery
cheapest cialis price
buy cheapest cialis on line
order cialis in canada
viagra tablet
viagra no online prescription
find cheap cialis online
viagra price
order viagra no prescription
cheap generic cialis
buy viagra online cheap
cialis uk
cialis without rx
generic cialis cheap
viagra vs cialis
order cialis on internet
viagra tablets
viagra purchase
impotence drugs
buy cialis generic
cialis tablet
cialis cheapest price
order viagra from canada
viagra generic
cheap viagra from canada
order cialis
compare viagra prices online
find cheap cialis
impotence cure
pfizer viagra
find discount cialis
cheapest cialis
buy cialis from india
impotence
buy cheapest viagra online
cialis side effects
viagra order
discount cialis online
cialis in malaysia
cialis in uk
viagra in uk
cialis online without prescription
cialis online pharmacy
order viagra
buy viagra online
viagra side effects
cialis sale
discount cialis no rx
cheapest viagra
find cialis
order cialis no rx
buy cialis low price
buy viagra cheap
drug cialis online purchase
order discount viagra online
50 mg viagra
100 mg viagra
10mg cialis
cost of cialis
cheapest cialis prices
buy discount viagra online
cialis sales
50mg viagra
cialis price
buy viagra on internet
cialis pill
cheapest cialis online
purchase viagra overnight delivery
cheap cialis from canada
cheapest viagra price
cialis 20 mg
buy sildenafil low cost
order viagra without prescription
buy viagra lowest price
no prescription cialis
order viagra on internet
discount cialis overnight delivery
cialis cheap drug
viagra approved
viagra no rx required
compare viagra prices
no rx cialis
cheap cialis on internet
buy viagra from india
buy discount cialis online
viagra pharmacy online
order viagra from us
cialis free delivery
cialis for order
buy cialis from canada
viagra without rx
viagra online review
10 mg cialis
cheap viagra no rx
cheapest viagra prices
viagra prices
cialis pharmacy
order no rx cialis
buy cialis in us
buy cialis no prescription required
order cialis from canada
lowest price cialis
cheap cialis internet
online pharmacy viagra
cheapest generic cialis
generic drugs
cialis india
find cialis without prescription
best price cialis
buy viagra without prescription
cheap cialis in uk
where to buy viagra
20 mg cialis
cheap cialis from uk
buy sildenafil canada
cialis no rx required
cialis in us
buy cialis overnight delivery
cialis cheap price
order cheap viagra online
20mg cialis
buy cheap viagra online
viagra internet
viagra without prescription
free cialis
buy cialis us
cialis buy
buy viagra in canada
order viagra cheap online
find viagra without prescription
viagra pills
cheap cialis no prescription
viagra online without prescription
order generic viagra
cialis discount
viagra cheapest price
purchase viagra no rx
viagra no rx
viagra cheap
discount viagra overnight delivery
sale cialis
cialis pharmacy online
purchase cialis without prescription
pharmacy online
cialis medication
discount viagra
buy cheap cialis
impotence medication
viagra medication
find cialis on internet
impotence pills
cialis prices
discount viagra without prescription
cialis online cheap
cialis online review
find cheap viagra online
buy viagra us
purchase cialis online
certified viagra
where to order viagra
buy cheapest viagra
buy cialis internet
order cialis online
buy sildenafil online
buy cialis cheap
cheap viagra
purchase cialis
find discount viagra
buy cialis on internet
cialis buy online
buy sildenafil online without a prescription
viagra buy online
order cheap cialis online
viagra information
no prescription viagra
cost of viagra
buy cialis in canada
buy cialis online
buy viagra
cheapest generic viagra
cialis us
cialis australia
fda approved cialis
lowest price for viagra
viagra bangkok
cialis prescription
cialis cost
buy no rx cialis
buy viagra internet
viagra discount
order viagra overnight delivery
generic cialis
viagra australia
25 mg viagra
order viagra online
viagra overnight
cialis rx
order cialis in us
order viagra no rx
order discount cialis online
viagra vendors
order viagra in us
buy sildenafil in uk
viagra us
buy generic viagra
viagra canada
viagra no prescription
viagra cheap price
cheap viagra tablet
viagra free delivery
overnight viagra
purchase viagra online
find cheap viagra
cialis malaysia
best price viagra
cialis free sample
find viagra on internet
cialis generic
buy sildenafil in canada
order cialis no prescription required
cheapest viagra online
purchase cialis no rx
viagra in us
order discount cialis
cheap viagra internet
free viagra
cialis approved
best price for cialis
cialis from india
find no rx viagra
generic viagra
viagra from canada
viagra online pharmacy
buy viagra from canada
cheapest generic viagra online
buy cheapest cialis
discount cialis
viagra overnight delivery
cialis without prescription
100mg viagra
cialis in australia
price of viagra
order cialis overnight delivery
cheap viagra in uk
buying generic cialis
viagra pill
buy cialis on line
low cost cialis
find discount viagra online
buying viagra
cheap cialis overnight delivery
pharmacy cialis
cheap viagra pill
viagra prescription
find viagra online
buy cialis lowest price
discount viagra no rx
online cialis
viagra free sample
cheap viagra in usa
find viagra
cheap viagra online
buy viagra no rx
generic viagra cheap
buy cialis without prescription
buy viagra in us
cheap viagra overnight delivery
cheap cialis in usa
cheap cialis online
viagra
order no rx viagra
viagra soft tab
find cialis online
lowest price viagra
cialis drug
cialis vendors
viagra online stores
erectile dysfunction
order viagra in canada
buy viagra on line
viagra overnight shipping
viagra online cheap
lowest price for cialis
approved viagra pharmacy
cialis 10 mg
cialis no online prescription
cialis purchase
cialis from canada
order cialis without prescription
viagra for sale
viagra in australia
approved cialis pharmacy
buy viagra generic
buy sildenafil in spain
find viagra no prescription required
cialis no prescription
buy viagra from us
order viagra no prescription required
cost viagra
purchase viagra without prescription
buy cialis no rx
cialis cheap
cialis internet
tablet viagra
cheap viagra on internet
viagra cost
pharmacy viagra
cialis soft tab
cialis information
buy cheap cialis internet
purchase cialis overnight delivery
cheap cialis without prescription
buy viagra no prescription required
compare cialis prices
buy cheap cialis online
overnight cialis
where to buy cialis
cheap cialis
buy cheap viagra internet
buy discount cialis
viagra buy drug
cheap viagra no prescription
buy sildenafil citrate
buying viagra online
buy discount viagra
fda approved viagra
cialis online stores
cheap cialis tablets
buy cheapest cialis online
cheap viagra tablets
order discount viagra
sale viagra
viagra online
cialis for sale
cialis soft
viagra pharmacy
buy cialis from us
viagra without a prescription
viagra in bangkok(Update: see also the C# version.)
More than a month ago I posted about implementing multidimensional hashtables in VB.NET by using Generics and descending a class from Hashtable. It had some limitations, however, including the fact that String wouldn’t work nicely as a value type in it. Well, the next day (or the day after) I figured out a different way to do things and never got around to posting it…until now. Allow me to introduce: DynamicDictionary.
''' <summary>
''' This class is identical to Dictionary except that
''' when you attempt to access a key that does not exist,
''' it will automatically populate that key with a newly-
''' created object of the appropriate type (v).
''' </summary>
''' <typeparam name="k">A Type to use as the key.</typeparam>
''' <typeparam name="v">
''' A Type to use as the value.
''' This Type must have an accessible parameterless
''' constructor.
''' </typeparam>
''' <remarks></remarks>
Public Class DynamicDictionary(Of k, v As New)
Inherits Dictionary(Of k, v)
Default Public Shadows Property Item(ByVal key As k) As v
Get
Dim ret As v = Nothing
' If the requested bucket is a null reference,
' create a new instance of whatever type
' belongs here: a new v.
If (Not MyBase.TryGetValue(key, ret)) Then
ret = New v()
MyBase.Add(key, ret)
End If
Return ret
End Get
Set(ByVal value As v)
MyBase.Item(key) = value
End Set
End Property
End Class
This class prevents the (seriously lame) default behavior of .NET’s Dictionary, which is to throw an exception when attempting to access a null bucket (instead of just returning the null reference, as a Hashtable would). Instead, it instantiates a new object of the appropriate type and returns that. To declare a three-dimensional “hashtable”, then, would look like this:
Dim myDictionary as New DynamicDictionary(Of Integer, _
DynamicDictionary(Of String, Double))
myDictionary(4)("key1") = 7.51
This new class solves a couple of problems the old one had. One such problem is that when using Hashtable, boxing/unboxing could still occur on value types. By descending from the new generic built-in Dictionary class, this new class should never do any boxing.
One issue that still remains is dealing with String, which has no accessible parameterless constructor and therefore cannot be used as the value argument of a DynamicDictionary. The only solution I’ve found in that case is to use a normal Dictionary instead:
Dim myDictionary as new DynamicDictionary(of Double, _
Dictionary(of Integer, String))
myDictionary(7.51)(4) = "something"
The drawback, of course, is that it will behave like a Dictionary and throw exceptions if you access a null bucket:
Dim myNullReference as String = myDictionary(999)(125) ' Throws exception!
If desired, you could get around this by creating a new class (”SafeDictionary”?) and using the TryGetValue() method I used above and instead of creating a new instance, simply return the null reference. (You could then also remove the New constraint from the class definition.)
To get true Hashtable behavior and return null references instead of new objects, I overloaded the Item property on DynamicDictionary to allow you to specify whether a new instance should be created.
Default Public Shadows Property Item(ByVal key As k, ByVal AutoCreateItem As Boolean) As v
Get
Dim ret As v = Nothing
If (AutoCreateItem) Then
ret = Item(key)
Else
MyBase.TryGetValue(key, ret)
End If
Return ret
End Get
Set(ByVal value As v)
MyBase.Item(key) = value
End Set
End Property

wow! so little code, such a huge impact!
thanks very much, very easy to use what you put together.
THANKS. GREAT AND SMART SOLUTION.
I made a little change ad try to mimic the real “Nothing” situation. I need the “nothing”. Just my 2 cents.
Public Class MyHashTable(Of k, v As New)
Inherits Dictionary(Of k, v)
End Class
Public Class MyNestedHashTable(Of k, v As New)
Inherits MyHashTable(Of k, v)
End Class
Usage:
Dim data_ As MyHashTable(Of String, MyNestedHashTable(Of String, Object)) = New MyHashTable(Of String, MyNestedHashTable(Of String, Object))()
data_.Item(”X”)(”Y”) = 123
data_.Item(”Z”)(”Y”) = 12346
MsgBox(data_.Item(”X”)(”Y”) + data_.Item(”Z”)(”Y”))
Dim obj As Object = data_.Item(”W”)(”Z”, True)
If obj Is Nothing Then
MsgBox(”Nothing Yet”)
End If