0

I have my class and a part of my code that use it on VB.NET

Public Class ResponseError
    Public code As String = ""
    Public message As String = ""
End Class

Public Class clsAPI
    Public lastError As New ResponseError

    Public Function addEntities(ByVal entities() As RequestAddEntity, ByVal a As clsCompany) As Boolean

        Try
            If Not isLogged() Then Throw New System.Exception("Error on login.")

            client.BaseUrl = New System.Uri(_serverUrl)

            Dim request = New RestRequest()
            request.Resource = {url}
            request.AddHeader("code", a.CompanyCode)
            request.AddHeader("token", _token)
            request.AddParameter("item", SimpleJson.SerializeObject(entities))
            request.Method = Method.POST

            Dim response As RestResponse = client.Execute(request)
            If response.ResponseStatus = ResponseStatus.Error Then
                lastError.code = response.ResponseStatus
                lastError.message = response.ErrorMessage
                Logger.Error("End addEntities() with error: " & lastError.message & " (" & lastError.code & ")")
                Return False
            End If

            Dim json = SimpleJson.DeserializeObject(Of ResponseLogin)(response.Content)
            If Not json.result Then
                For Each error In json.errors
                    lastError = error
                    Logger.Error("End addEntities() with error: " & lastError.message & " (" & lastError.code & ")")
                    Return False
                Next
            End If

            Logger.Trace("End addEntities()")
            Return True

        Catch ex As Exception
            lastError.code = 0
            lastError.message = ex.Message
            Logger.Error("End addEntities() with exception: " & lastError.message & " (" & lastError.code & ")")
            Return False
        End Try
    End Function
End Class

But sometimes I have this error on my log:

2020-02-03 16:48:13.2902|ERROR|clsAPI.addEntities|End addEntities() with exception: Invalid cast from 'System.String' to 'ETAgentI4U.ResponseError'. (0)

Do you know the meaning of it? How can I solve it?

Thanks :)

Swim89
  • 290
  • 8
  • 28
  • It means that somewhere in your code, you have a `String` or variable of type `String` and you are trying to assign it's value to a variable of type `ResponseError`. The system is however unable to reconcile the two – Anu6is Feb 03 '20 at 17:48
  • 2
    If it is a design-time error, then [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) will point it out for you. – Andrew Morton Feb 03 '20 at 18:34
  • 1
    I think you must be missing something from your example here. As far as I can see ResponseError isn't even used in the addEntities method. Can you give use a complete sample? – Hursey Feb 04 '20 at 04:27
  • I'd pay particular attention to the line assigning `lastError = error` (since this most closely corresponds to the error message), and I'd second the recommendation from Andrew to use `Option Strict On` as it will almost certainly flag the assignment that is causing the issue. – Craig Feb 04 '20 at 13:59
  • Thanks to all of you! I tried to turn on Option Strict by I didn't get changes. I corrected my code adding implementation of ResponseError class. – Swim89 Feb 06 '20 at 17:43
  • 1
    @Swim89 Perhaps you set Option Strict On to be the default for *new* projects. You also need to set it on for the current project, as shown at the end of the answer I linked to. Another way to force it is to add `Option Strict On` as the first line of the code, before any `Imports` or anything else. – Andrew Morton Feb 07 '20 at 09:16

0 Answers0