1

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!

0

3 Answers 3

2

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));
Sign up to request clarification or add additional context in comments.

3 Comments

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"
Umm, MSDN says it exists. Are you including the right Imports statement for System.Collections.Generic? msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx
Doh! I'd imported Specialized not Generic. Thanks!
0

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")

Comments

0

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)

3 Comments

He's lose his sytnactic sugar, though.
You're right. But then again there is: val = dic.get(new MultiKey("key1").("key2"))
Unless I'm missing something you'd never be able to get it out. You can make a new key object from the same values, but it wouldn't point to the same object in the dictionary because it's a different object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.