Creating a multi-dimensional hashtable with generics in VB ASP.NET?
-
02-07-2019 - |
Question
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!
Solution
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));
OTHER TIPS
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")
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)