0

I created my data dictionary:

Dim data As New Dictionary(Of String, String)
data.Add("customer", "TEST")
data.Add("secretKeyId", "2222222222222")
data.Add("secretKey", "333333333333333")

and I have the following function:

 Public Shared Function Create(ByVal data As Dictionary(Of String, Object)) As [String]
    ' Serializes the body into JSON using the fastJSON library.
    Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(data)
    Dim body As Byte() = Encoding.UTF8.GetBytes(json) ....

How can I call the Create function and pass the DATA with all the keys and values that I entered to this function?

I tried: create(data.all) or create(data) but it is not working.

freefaller
  • 19,368
  • 7
  • 57
  • 87
user3570022
  • 85
  • 1
  • 11
  • Please learn how to format your questions - which can be found using the [help pages](http://stackoverflow.com/editing-help) – freefaller Apr 24 '14 at 17:48
  • 3
    can you expand on "its not working"; mindreading skills are at the cleaners, but why is it `Dictionary(Of String, String)` one place and `Dictionary(Of String, Object)` in the other? – Ňɏssa Pøngjǣrdenlarp Apr 24 '14 at 17:49
  • I think, this is not supported based on an old answer here - http://stackoverflow.com/questions/2149589/idictionarytkey-tvalue-in-net-4-not-covariant. Has anything changed on this? – shahkalpesh Apr 24 '14 at 18:06

1 Answers1

0

You are trying to pass an incompatible data type to your method: in your caller method your dictionary is of type string, string and your function awaits dictionary of type string, object. You have two solutions:

  • either change the signature of your dictionary in the main part to Dim data As New Dictionary(Of String, Object)
  • or change the signature to the dictionary parameter of the method to ByVal data As Dictionary(Of String, String)

A possible solution looks like this:

Public Shared Function Create(ByVal data As Dictionary(Of String, String)) As [String]
    ' Serializes the body into JSON using the fastJSON library.
    Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(data)
    return json
end function

The call:

Dim data As New Dictionary(Of String, String)
data.Add("customer", "TEST")
data.Add("secretKeyId", "2222222222222")
data.Add("secretKey", "333333333333333")
Dim dataAsJson = Create(data)
Console.WriteLine(dataAsJson)

The output:

{"customer":"TEST","secretKeyId":"2222222222222","secretKey":"333333333333333"}

This would also work correctly in this case and create the same result as above:

Public Shared Function Create(ByVal data As Dictionary(Of String, Object)) As [String]
    ' Serializes the body into JSON using the fastJSON library.
    Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(data)
    return json
end function

Dim data As New Dictionary(Of String, Object)
data.Add("customer", "TEST")
data.Add("secretKeyId", "2222222222222")
data.Add("secretKey", "333333333333333")
Dim dataAsJson = Create(data)
Console.WriteLine(dataAsJson)
keenthinker
  • 7,645
  • 2
  • 35
  • 45