locked
Option Strict Off in VB induces sloth? RRS feed

  • Question

  • User759549661 posted

    Hi folks,

    I recently added a VB logic module to my Visual Studio 2017 application, containing public functions to retrieve visitor locations from IP addresses.  When deployed, the app times out, even though I'm the only one looking at it. My ISP provider suggested that the cause could be that an invalid object variable is being referenced, the fix being to set Option Strict On. My App-wide setting is Option Strict Off, and until I added this module the deployed app was fast, no problem. If I set Option Strict On in the new module, Intellisense picks out some "late binding" which it disallows. Specifically, on the last line of:

    Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
    HttpWReq = CType(WebRequest.Create(strQuery), HttpWebRequest)
    HttpWReq.Method = "GET"
    HttpWResp = CType(HttpWReq.GetResponse(), HttpWebResponse)
    Dim reader As IO.StreamReader = New IO.StreamReader(HttpWResp.GetResponseStream())
    Dim content As String = reader.ReadToEnd()
    Dim item As Object = serializer.Deserialize(Of Object)(content)
    Dim city As String = item("cityName")

    Where strQuery = "http://api.ipinfodb.com/v3/ip-city/?" & "ip=" & UserIP & "&key=" & ipinfokey & "&format=json"

    The problem seems to be that there is no explicit conversion of the object "item" to a string.

    Intellisense directed me to change

    Dim city As String = item("cityName")

    To:

    Dim city As String = GetCity(item)

    Where GetCity is:

    Private Shared Function GetCity(item As Object) As String
    Return CType(item("cityName"), String)
    End Function

    I did that, but Intellisense is still unhappy with "item" in the GetCity function, because it disallows "late binding".

    FYI, the value of item("cityName") is "-" if the city is not found.

    Is there a way to state this in VB that satisfies the requirements of Option Strict On?

    Is this even a possible cause of having the app time out?

    Monday, May 11, 2020 4:22 PM

Answers

  • User753101303 posted

    Hi,

    This is because of:

    Dim item As Object = serializer.Deserialize(Of Object)(content)

    Instead you coild likely declare a class with a cityName property and use:

    Dim item As MayClass= serializer.Deserialize(Of MyClass)(content)
    Dim city As String = item.CityName

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 4:55 PM

All replies

  • User753101303 posted

    Hi,

    This is because of:

    Dim item As Object = serializer.Deserialize(Of Object)(content)

    Instead you coild likely declare a class with a cityName property and use:

    Dim item As MayClass= serializer.Deserialize(Of MyClass)(content)
    Dim city As String = item.CityName

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 4:55 PM
  • User759549661 posted

    Thanks PatriceSc, Intellisense likes your solution: no red marks with Option Strict On!

    Rob

    Monday, May 11, 2020 7:24 PM