Search code examples
c#vb.netjson-rpccode-conversion

After converting from C# to vb.net it shows an error


I am trying to convert C# code to Visual Basic.

This is the C# code:

public static string RequestServer1(string methodName, List<string> parameters)
{
    return RequestServer(methodName, parameters.Select(p => new JValue(p)).ToList<JToken>());
}

And this is the VB.Net code:

Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of String)) As String
    Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).ToList())
End Function

And here the function with List(Of JToken):

Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of JToken)) As JToken
    Dim ServerIp As String = "http://localhost:8332"
    Dim UserName As String = "hama"
    Dim Password As String = "hama"
    Dim webRequest As HttpWebRequest = CType(webRequest.Create(ServerIp), HttpWebRequest)
    webRequest.Credentials = New NetworkCredential(UserName, Password)

    webRequest.ContentType = "application/json-rpc"
    webRequest.Method = "POST"

    Dim respVal As String = String.Empty
    Dim joe As JObject = New JObject
    joe.Add(New JProperty("jsonrpc", 1))
    joe.Add(New JProperty("id", 1))
    joe.Add(New JProperty("method", methodName))
    Dim props As JArray = New JArray
    For Each parameter In parameters
        props.Add(parameter)
    Next
    joe.Add(New JProperty("params", props))
    ' serialize json for the request
    Dim s As String = JsonConvert.SerializeObject(joe)
    Dim byteArray() As Byte = Encoding.UTF8.GetBytes(s)
    webRequest.ContentLength = byteArray.Length
    Dim dataStream As Stream = webRequest.GetRequestStream
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim streamReader As StreamReader = Nothing
    Try
        Dim webResponse As WebResponse = webRequest.GetResponse
        streamReader = New StreamReader(webResponse.GetResponseStream, True)
        respVal = streamReader.ReadToEnd
        Dim data = JsonConvert.DeserializeObject(respVal).ToString
        Return data
    Catch exp As Exception

    Finally
        If (Not (streamReader) Is Nothing) Then
            streamReader.Close()
        End If
    End Try

    Return String.Empty
End Function

Sometimes I need a list of JToken, and sometimes I need a list of string, so my both methods can work.

Before compile it shows an error, it doesn't accept Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).ToList())

It says:

Impossible to convert a List (Of JValue) to a List (Of String)

What I am doing wrong here?


Solution

  • The C# code was converting a List(Of JValue) to a List(of JToken), but this conversion was lost in the translation of C# to VB.

    If JValue inherits JToken, then you can use Enumerable.Cast to cast your enumerable from one type to another (before calling .ToList()):

    Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of String)) As String
        Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).Cast(Of JToken).ToList())
    End Function