84

Very easy today, I think. In C#, its:

Dictionary<String, String> dict = new Dictionary<string, string>() { { "", "" } };

But in vb, the following doesn't work.

Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) (("",""))

I'm pretty sure there's a way to add them at declaration, but I'm not sure how. And yes, I want to add them at declaration, not any other time. :) So hopefully it's possible. Thanks everyone.

I've also tried:

Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) ({"",""})

And...

Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) {("","")}

And...

Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) {{"",""}}
XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59

4 Answers4

137

This is possible in VB.NET 10:

Dim dict = New Dictionary(Of Integer, String) From {{ 1, "Test1" }, { 2, "Test1" }}

Unfortunately IIRC VS 2008 uses VB.NET 9 compiler which doesn't support this syntax.

And for those that might be interested here's what happens behind the scenes (C#):

Dictionary<int, string> VB$t_ref$S0 = new Dictionary<int, string>();
VB$t_ref$S0.Add(1, "Test1");
VB$t_ref$S0.Add(2, "Test1");
Dictionary<int, string> dict = VB$t_ref$S0;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • fine! I wonder how that behaves, does it build the two dimensional array first, and then it copies to the dictionary? – vulkanino Sep 22 '10 at 17:34
  • Dern. So you're saying that since I'm using VS2008 I can't do it? That stinkies. – XstreamINsanity Sep 22 '10 at 17:43
  • 3
    Yeap, a bit stinky :-) Time to upgrade. – Darin Dimitrov Sep 22 '10 at 17:44
  • Is there any way to convert a two dimensional array to a dictionary? If so, I'll just do that. My goal is that I have ViewModes and each mode has a separate query. I wanted to iterate through the dictionary, add the key to a view mode combo box, and the value would be the query. Or something of the sort. Maybe I'll have to use a two-dimensional array. – XstreamINsanity Sep 22 '10 at 17:47
  • I ended up just making it a property and adding them in there. Don't know why I didn't think of it before. – XstreamINsanity Sep 22 '10 at 18:59
  • With how it works behind the scenes, does `VB$t_ref$S0` have the same scope as `dict`? is `VB$t_ref$S0` Garbage Collected immediately? What if `dict` was a public property? – Luke T O'Brien Mar 28 '17 at 11:06
17

It is much the same, use the From keyword:

    Dim d As New Dictionary(Of String, String) From {{"", ""}}

However, this requires version 10 of the language, available in VS2010.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
14

Here's a cool translation: You can also have a Generic Dictionary of strings and string array.

C#:

private static readonly Dictionary<string, string[]> dics = new Dictionary<string, string[]>
{
    {"sizes", new string[]  {"small", "medium", "large"}},
    {"colors", new string[]  {"black", "red", "brown"}},
    {"shapes", new string[]  {"circle", "square"}}
};

VB:

Private Shared ReadOnly dics As New Dictionary(Of String, String()) From {
 {"sizes", New String() {"small", "medium", "large"}},
 {"colors", New String() {"black", "red", "brown"}},
 {"shapes", New String() {"circle", "square"}}}

Cool haa :)

Andreas
  • 5,393
  • 9
  • 44
  • 53
Mike
  • 139
  • 1
  • 3
1

There's no constructor to take a KeyValuePair for a dictionary.

vulkanino
  • 9,074
  • 7
  • 44
  • 71