0

I have the line of code that I want which is:

mid= jToken.Value<double?>("mid") ?? 100; 

in C# but I need it in VB.NET I got it from Get value from JToken that may not exist (best practices)

But I'm have a bit of trouble in converting that to the proper syntax in VB. I've tried

Dim mid As String = item.Value(Of String)("mid") ?? "" 

But it does not like the?

What I would like is to end with an empty string or blank if the value is not in the object. This is my full code

Dim obj As JObject = JObject.Parse(respHTML)
Dim records As JArray = DirectCast(obj("records"), JArray)
For i As Integer = 0 To records.Count - 1
    Dim item As JObject = DirectCast(records(i), JObject)
    Dim pmid As Integer = item("pmid").Value(Of Integer)
    Dim pmcid As String = item("pmcid").Value(Of String)
    Dim doi As String = item("doi").Value(Of String)
    '  Dim mid As String = item("mid").Value(Of String)
    Dim mid As String = item.Value(Of String)("mid") ?? ""   
    MessageBox.Show(pmid.ToString + " " + pmcid + " " + doi + " " + mid)
Next
Supriya
  • 290
  • 4
  • 15
Bill
  • 1,423
  • 2
  • 27
  • 51
  • Are you looking for [Is there a VB.NET equivalent for C#'s '??' operator?](https://stackoverflow.com/q/403445/3744182) and/or [VB.NET null coalescing operator?](https://stackoverflow.com/q/6792729/3744182)? – dbc Apr 08 '18 at 18:57
  • I tried Dim mid As String = item.Value(Of String)("mid") and it return nothing if blank and that is close enough. If you have a better solution I'd love to see it but if not this can work for me. – Bill Apr 08 '18 at 19:01

1 Answers1

0

I ended up with:

        Dim obj As JObject = JObject.Parse(respHTML)
        Dim records As JArray = DirectCast(obj("records"), JArray)
        For i As Integer = 0 To records.Count - 1
            Dim item As JObject = DirectCast(records(i), JObject)
            Dim pmid As String = If(item.Value(Of String)("pmid"), "")
            Dim pmcid As String = If(item.Value(Of String)("pmcid"), "")
            Dim doi As String = If(item.Value(Of String)("doi"), "")
            Dim mid As String = If(item.Value(Of String)("mid"), "")
            MessageBox.Show(pmid.ToString + " " + pmcid + " " + doi + " " + mid)
        Next

The message box line will be replaced with a database call

Bill
  • 1,423
  • 2
  • 27
  • 51