1

We can do this in vb.net:

Dim d= new Dictionary(of string, string) from {{"a","valA"},{"b","valB"}}

Please how can we make the following possible for convenience:

public sub Setup(args)
   Dim d= new Dictionary(of string, string) from args
end sub

Thanks.

Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
  • 1
    what is the type of `args`, preferably? – Baby Jan 15 '15 at 08:42
  • 2
    No, you can't initialize a dictionary with collection initializer syntax from a variable or parameter. It's just syntactic sugar that the compiler _eats_ if you assign it in the way you've shown in the first snippet. However, you can pass an `IDictionary(Of String, String)` and use that for the constructor of the dictionary. Or you can pass an `IEnumerable(Of KeyValuePair(Of String, String))` and use that for `args.ToDictionary(Function(kv) kv)`. The former approach is more efficient, the latter is more general. – Tim Schmelter Jan 15 '15 at 09:03
  • @TimSchmelter that sucks... the initializer convenience would have made all the difference – Charles Okwuagwu Jan 15 '15 at 09:10
  • I think second Tim's solution is easyest .. KeyValuePair – CristiC777 Jan 15 '15 at 09:10
  • @CharlesO: Why? What do you want to pass as parameter at all? Every methiod parameter has a concrete type, what do you want to pass to initialze a dictionary? – Tim Schmelter Jan 15 '15 at 09:12
  • @TimSchmelter i was thinking simple {{"",""},....} – Charles Okwuagwu Jan 15 '15 at 09:13
  • @TimSchmelter that would simply be string ()() – Charles Okwuagwu Jan 15 '15 at 09:14
  • @CharlesO: That's neither a type nor valid syntax (taken by itself). – Tim Schmelter Jan 15 '15 at 09:14
  • @TimSchmelter i get what you are saying. i felt if i can write this: `Dim d= new Dictionary(of string, string) from {{"a","valA"},{"b","valB"}}` then the from part could be made into a variable. but you have pointed out that is is just syntactic sugar...sadly – Charles Okwuagwu Jan 15 '15 at 09:17

1 Answers1

2

No, you can't initialize a dictionary with collection initializer syntax from a variable or parameter. It's just syntactic sugar that the compiler eats if you assign it in the way you've shown in the first snippet.

However, you can pass an IDictionary(Of String, String) and use that for the constructor of the dictionary:

Public sub Setup(dict As IDictionary(Of String, String))
   Dim d = new Dictionary(Of String, String)( dict )
End Sub

Or you can pass an IEnumerable(Of KeyValuePair(Of String, String)) and use that for args.ToDictionary(Function(kv) kv):

Public sub Setup(keyVals As IEnumerable(Of KeyValuePair(Of String, String)))
   Dim d = args.ToDictionary(Function(kv) kv)
End Sub

The former approach is more efficient, the latter is more general. Allowing IDictionary(Of TKey, TValue) has the advantage that you can pass more types since there are many classes that implement that interface.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Question: is this a vb.net limitation? does c# offer an alternative? – Charles Okwuagwu Jan 15 '15 at 09:21
  • @CharlesO: no, in C# it's also not possible. But again, i don't know what you have in mind. `{{"a","valA"},{"b","valB"}}` is not a tye and not valid syntax if not used directly as collection initializer for a dictionary. – Tim Schmelter Jan 15 '15 at 09:22
  • I'm trying to simplify an API which requires initializing a dictionary(of string,string). IT becomes very tedious to keep passng new Dictionary(of string, string) from {{"a","valA"},{"b","valB"},....} as arguments each time the the function is called , and it is used a lot. – Charles Okwuagwu Jan 15 '15 at 09:26
  • @CharlesO: why don't you reuse the dictionary instead of always creating a new one? It's not clear what the input is, if it's a collection you could also use LINQ's `ToDictionary`. – Tim Schmelter Jan 15 '15 at 09:28