Search code examples
vb.netequalsequals-operator

Comparing strings not working properly vb.net


I'm trying to compare 2 strings (KeyCode and SerialN) I've tried a bunch of diferente aproaches... but as you can see in my print screen neither = or Equals work.

Can you tell me what is wrong?

Private Function VerificaKeyCode(SerialN As String) As Boolean
        Dim snEsq As Integer
        Dim snDir As Integer
        Dim KeyCode As String
        Dim buffer As String
        Dim ind As Short

        VerificaKeyCode = False
        buffer = Space(255)
        For ind = 1 To 5
            'UPGRADE_WARNING: App property App.EXEName has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6BA9B8D2-2A32-4B6E-8D36-44949974A5B4"'
            GetPrivateProfileString(My.Application.Info.AssemblyName, "HD" & ind, "", buffer, Len(buffer), My.Application.Info.DirectoryPath & "\ServerHD.ini")
            KeyCode = Trim(buffer)
            If KeyCode <> "" Then
                VerificaKeyCode = KeyCode.Equals(SerialN)
                VerificaKeyCode = CBool(KeyCode = SerialN)
                If VerificaKeyCode Then Exit Function
            End If
        Next ind
    End Function

enter image description here

Edit

Aparently there is an empty char in the string, I need to remove it somehow

enter image description here


Solution

  • The expressions in your watch window are stale as indicated by the disabled color and the presence of the Refresh/Recycle icon. Click the icon and the expression will be re-evaluated and the result updated:

    enter image description here

    Mimicking what you have, notice the first expression is stale and wrong. The second has been refresh/reevaluated and reports the correct result. As soon as you execute a line of code, the watch expressions will be marked as stale/not updated.


    A better way to return a function with NET is to use a local variable and Return it. The local var allow you to easily view the result by hovering the mouse:

    enter image description here

    A variable can go out of scope, but it can't be stale.


    Edit 4 or 5 reveals that while the watch expressions are stale, so what we see in the pic neither proves nor disproves that the strings do not match, this is apparently a VB6 -> .NET refurb (notice the 102 warnings).

    We cant see the declaration for GetPrivateProfileString but the usage is at least suboptimal.

    <DllImport("kernel32.dll", SetLastError:=True)>
    Shared Function GetPrivateProfileString(appname As String,
                                            keyname As String, def As String,
                                            sbuffer As StringBuilder, 
                                            nSize As Int32,
                                            strFile As String) As Integer
    End Function
    

    Note that it is a Function; it returns the number of chars read into the buffer. Rather than Trim which will not remove NUL, the return should be used (if not a stringbuilder) to get the number of characters specified. If you use a string buffer, trim to the number of characters read:

    Dim n As Int32 = GetPrivateProfileString(...)
    KeyCode = buffer.Substring(0, n)
    

    StringBuilder should do that for you:

    Dim buffer As New StringBuilder(255)
    ...
    Dim n As Int32 = GetPrivateProfileString(..., buffer, buffer.Capacity)
    KeyCode = buffer.ToString()
    

    You can still check the function return against the size of the string.


    You could skip GetPrivateProfileString entirely and read the file yourself with a stream reader. It should be faster than PInvoke, but at the least, less convoluted to use.