2

When I try to pass a Dictionary(Of String, Object) to a function parameter that wants a Dictionary(Of String, String) I get the following error:

Unable to cast object of type 'System.Collections.Generic.Dictionary'2[System.String,System.Object]' to type 'System.Collections.Generic.Dictionary'2[System.String,System.String]'.

All of the Object values in the dictionary are strings but the dictionary was declared as String/Object. I would have thought that the system would be able to convert this but since it isn't I need to do it myself.

I looked at the .ToDictionary() prototype method but all of the examples show a list being converted to a dictionary.

I found this question which has an accepted answer for what I want but it's written in C# and I can't figure out the conversion to VB.Net.

Edit 1

Offending code. Obviously boiled down or else I would just simply declare dict1 as string/string in my actual code.

Dim dict1 As New Dictionary(Of String, Object) From {{"key1","value1"}}

SomeFunctionThatExpectsParamToBeDictOfStringString(dict1)

Edit 2

I tried:

SomeFunctionThatExpectsParamToBeDictOfStringString(dict1.ToDictionary(Function(k) k.Key, Function(v) v.Value.ToString()))

but got:

System.MissingMemberException: Public member 'ToDictionary' on type 'Dictionary(Of String,Object)' not found.

5
  • Please post the offending code. Commented Feb 25, 2020 at 21:28
  • If you really know every item is a string, the best solution here is to change the declaration of the dictionary. Commented Feb 25, 2020 at 21:52
  • Despite the example given, It's not as simple as that. Commented Feb 25, 2020 at 21:59
  • Regardless of the fact that you know all of the Objects are really Strings, the type system doesn't know that (if it did, it would be a Dictionary(Of String, String) rather than a Dictionary(Of String, Object)). And while it might make sense to treat a dictionary from string to string as a dictionary from string to object, it really doesn't make sense to go in the other direction, because the dictionary can legally contain things that aren't strings. Commented Feb 26, 2020 at 14:27
  • The dictionary is generated inside of, and returned from a method. Sometimes this dictionary does need to be a string/object because the objects are a mix of strings, integers, and decimals. In other cases, it is a string/object where the objects are all strings. Regardless, I should be able to convert the dict of string/object to string/string since all of the potential objects can easily be converted to a string value. It just so happens that I only need to convert this dict of string/object when all the objects are strings but again, it shouldn't matter if that was not the case either. Commented Feb 26, 2020 at 14:42

1 Answer 1

5

This could be the VB.NET version of the C# code you have linked

Dim dic1 As Dictionary(Of String, Object) = New Dictionary(Of String, Object) 
dic1.Add("A", "B")

Dim dic2 As Dictionary(Of String, String) = dic1.ToDictionary(Function(k) k.Key, 
                                                              Function(v) v.Value.ToString())
Sign up to request clarification or add additional context in comments.

7 Comments

Please see Edit2
That's seems to be a missing Imports System.Linq
Same error. I don't think its related but the dictionary I am trying to convert is the result of a Union of two other dictionaries: dict1.Union(dict2).ToDictionary(Function(d) d.Key, Function(d) d.Value)
.ToDictionary doesn't change dic1. It produces a new dictionary. Trying to pass dic1 will not have desired result. Try assigning to a new variable of the proper type before passing to your function
@Steve, I agree this should be working. I am accepting your answer. As far as something happening dynamically, the dictionary is being built and returned from a function. When debugging, the returned dictionary looks like this: System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair(Of String, 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.