views:

2631

answers:

3

I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax like

val = myHash("Key").("key")

I know I need to use Generics but I can't figure out the correct syntax using VB in ASP.NET 2.0, there are plenty of c# examples on the net but they aren't helping much.

Cheers!

+2  A: 

OK, I'm better at C# than vb.net, but I'll give this a go....

Dim myHash as Dictionary(Of string, Dictionary(Of string, Integer));
James Curran
That looks right to me, but I too am way more of a C# person than VB.NET...
Yadyn
Unfortunately, there is no Dictionary type in VB.NET as far as I can see. IntelliSense doesnt like that snippet and wants to change it to ListDictionary or HybridDictionary, both of which then give the error message: "... has no type parameters and so cannot have type arguments"
Adam Pope
Umm, MSDN says it exists. Are you including the right Imports statement for System.Collections.Generic? http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx
Yadyn
Doh! I'd imported Specialized not Generic. Thanks!
Adam Pope
A: 

There's also the System.Collections.Specialized.StringDictionary(Of T) collection, which is just a pre-defined Dictionary(Of String, T).

And the syntax to use either the normal Dictionary or the StringDictionary would look like this:

val = myHash("key")("key")

Not like this:

val = myHash("key").("key")
Joel Coehoorn
+1  A: 

Consider that you may only need to use Dictionary, and that can compose your multiple keys into a single key object with its own composite hash code. E.g. make a multikey class and then use it as the key.

in pseudocode:

class Multikey {
 private keys;
 public setKey1(...)
 public setKey2(...)
}
Dim myKey as MultiKey(...)
myKey.key1 = ...
myKey.key2 = ...

Dim mydic as Dictionary(Of MultiKey, Integer)

val = mydic(myKey)
Josh
He's lose his sytnactic sugar, though.
Joel Coehoorn
You're right. But then again there is: val = dic.get(new MultiKey("key1").("key2"))
Josh