views:

66

answers:

3

I'd like to create a Dictionary that is indexed by Strings: Dictionary(of String, ...)

I'd like the Type after the comma to be an Array of MyObject's.

If all I do is the following:

Dim D as new Dictionary(of String, Array)

I feel like I'm missing out on some performance very time I access a member:

Dim Object1 as MyObject = MyDictionary("Key1")(4)

Doesn't it have to perform some type of lookup to figure out what type of object the array is holding every time I access it in this manner?

+2  A: 

Using array, you will be invoking type casting when you retrieve it. How about this:

Dim D as new Dictionary(of String, List(of MyObject))
o.k.w
Depending on how you want to work with the array, `Dim D as New Dictionary(Of String, MyObject())()` as suggested by other posts might be a better alternative. :P
o.k.w
+3  A: 

If the Value really needs to be an array of MyObject, you could do the following:

Dim D as new Dictionary(of String, MyObject())
Yannick M.
+2  A: 

Array isn’t what you want; rather, use the strongly typed array that you want to have:

Dim D as New Dictionary(Of String, MyObject())()

This works like a charm.

Konrad Rudolph
I think you have an extra () at the end :)
hamlin11
@hamlin11: No, I don’t. The parentheses belong there: It’s a constructor *call*! VB doesn’t require parentheses on method calls (only function calls with parameters) but the IDE will add them automatically in most (not all) cases. The above is one case where parentheses are not added, which is a shame because it’s inconsistent. I prefer code consistency, therefore I tend to add parantheses on all method calls.
Konrad Rudolph