Home > Mobile >  String equals nothing but also does not equal nothing. Variable cannot be set
String equals nothing but also does not equal nothing. Variable cannot be set

Time:12-31

The large scope of the code is showing a .net form in a VB6 application. The small issue I'm having is setting the two properties for the form. This logic works with another form but for some reason I cannot set Device(input parameter 1) or Scanner(input parameter 2) on the second form.

       'working case statement
       Case ScannerEdit
            Dim Device As String = String.Empty
            Dim Scanner As Integer = 0

            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
                Device = formInputParameters(0)
            End If
            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 2 Then
                Scanner = formInputParameters(1)
            End If

            Dim f As frmScannerEdit = base
            f._Device = Device
            f._Scanner = Scanner

        'not working case statement
        Case ScannerCommandsList

            Dim Device As String = String.Empty
            Dim Scanner As Integer = 0

            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
                Device = formInputParameters(0)
            End If
            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 2 Then
                Scanner = formInputParameters(1)
            End If

            Dim f As frmScannerCommandsList = base
            f._Device = Device
            f._Scanner = Scanner

While running in debug, the code creates Device and sets it to Nothing, then when the parameter set comes instead of setting it to "TestDevice" it stays at nothing. Same with scanner.

I've tried to put

If Device = Nothing Then
    Device = "TestDevice"
End If

within the set Device if statement but it didn't recognize that as being true; even though Device = Nothing, the statement concluded as false. Why can I not set a variable and why does the variable not equal Nothing when the variable does equal Nothing?

CodePudding user response:

The string variable Device doesn't equal Nothing, it is initialized to String.Empty. That's different.

Try changing the If statement to test for an empty string, eg

If Device.Length = 0 Then

or

If Device.Equals(String.Empty) Then

CodePudding user response:

For strings, you can use String.IsNullOrEmpty() or String.IsNullOrWhitespace() for this kind of check.

Additionally, it's possible to create a array with actual elements (the array is not Nothing and has the required length), but then still assign a Nothing value to one of the array elements. For example, given this array and the exact same code from the question, Device would definitely end up as Nothing:

Dim formInputParameters() As String = {Nothing, "0"}

If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
    Device = formInputParameters(0)
End If

Finally, I see Scanner is declared as an Integer, while Device is a String, but both are assigned values out of the same array (of unknown type, since we don't see where it's declared). That's not okay! It indicates to me you are not using Option Strict. It's not 1997 anymore. Option Strict should be turned on!

When you do set this option you'll find a bunch of new compiler errors, and be tempted to turn it back off. Don't do that! Every one of those errors is a place where your program has the potential to fail at run time, and they'll pretty much all be easy fixes. Take the time to make those adjustments and your program will be all the more stable and faster for the effort.

  • Related