[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Need some help with an empty string

Experts, I have some functions that return information from an industrial printer. Every now and again there is an error and the laser sends empty data. I have done everything I can think of to catch this but an empty string still gets through. Can anyone please help me on this.

Please follow along on the screen shots.

In this screen shot you can see where the error occurred because the string is empty when it tries to perform functions on the string.

Screen Shot
In this screen shot you can see that i have verified the empty string.

 Screen Shot
In this screen you can see where I check the value of objSocket.strStatus before the processStatus method is called. How is this getting past the check? I have checked for vbNullstring, Nothing, String.Empty and none of them catch this.

Screen Shot
0
Basicfarmer
Asked:
Basicfarmer
  • 9
  • 7
  • 2
1 Solution
 
p_davisCommented:
in c#'s string object there is a static method for this

String.IsNullOrWhitespace(yourstring)

there should be something similar in vb and you can use that in an if statement.
0
 
p_davisCommented:
String.IsNullOrEmpty(yourstring)
0
 
p_davisCommented:
sorry empty makes more sense.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
p_davisCommented:
just checked and it does exist in vb too.
0
 
BasicfarmerAuthor Commented:
I'll check that right now.
0
 
BasicfarmerAuthor Commented:
I get the exact same results.
0
 
p_davisCommented:
new code please?
0
 
BasicfarmerAuthor Commented:
    Private Sub socketWorker_Completed(sender As Object, e As RunWorkerCompletedEventArgs) Handles socketWorker.RunWorkerCompleted

        If e.Cancelled = False Then
            If Not e.Result Is Nothing Then
                Dim objSocket As clsSocket = CType(e.Result, clsSocket)
                Debug.Print(objSocket.strBackup)
                Debug.Print(objSocket.strErrors)
                If objSocket.strStatus = "SOCKET ERROR" Or objSocket.strErrors = "SOCKET ERROR" Then
                    RaiseEvent onSocketError(Me, New eArgs(ipAddress))
                ElseIf String.IsNullOrEmpty(objSocket.strStatus) Or String.IsNullOrEmpty(objSocket.strErrors) Then
                    RaiseEvent onDataError(Me, New eArgs(ipAddress))
                Else
                    Debug.Print(objSocket.strStatus)
                    processStatus(objSocket.strStatus)
                    processError(objSocket.strErrors)
                End If
                If backupCmd = True Then
                    If objSocket.strBackup = "SOCKET ERROR" Then
                        RaiseEvent onSocketError(Me, New eArgs(ipAddress))
                    ElseIf objSocket.strBackup = vbNullString Then
                        RaiseEvent onDataError(Me, New eArgs(ipAddress))
                    Else
                        processBackup(objSocket.strBackup)
                    End If
                    backupCmd = False
                End If
                If jetCmd = True Then
                    jetCmd = False
                End If
                objSocket = Nothing
            End If
        End If
        socketWorker.Dispose()
    End Sub

Open in new window

0
 
p_davisCommented:
you should probably check those two strings in different if statements
0
 
BasicfarmerAuthor Commented:
Ok look what I did, i went right to the processStatus method. Look at the if statement I out in. It did not catch it. Look at the immediate window. What the heck?
 Screen Shot
0
 
Fernando SotoCommented:
Hi Basicfarmer;

Can you get the string length before using it and see what the value is when it fails.

Dim strSize As Integer = objSocket.strStatus.Length
0
 
p_davisCommented:
where does the error occur now?
0
 
BasicfarmerAuthor Commented:
Yes, strSize = 1025
0
 
BasicfarmerAuthor Commented:
If it helps any here is my socket operation that returns the data.
This sub is in the class that objSocket references.
        Sub getStatus()
            Dim bytes() As Byte = Encoding.ASCII.GetBytes("SU" & Chr(13))
            Dim recBuffer(1024) As Byte
            Try
                tSocket.Send(bytes)
                tSocket.Receive(recBuffer)
                _status = Encoding.ASCII.GetString(recBuffer)
            Catch
                tSocket.Close()
                _status = "SOCKET ERROR"
            End Try
        End Sub

Open in new window

0
 
Fernando SotoCommented:
So the string is NOT empty it most likely has 1025 non printable characters in it. When the program throws the exception in the Watch window view the variable. Right click in that window and switch to Hex mode then you should see all the characters.
0
 
BasicfarmerAuthor Commented:
When the exception is thrown the result in the watch window is "" even in hex. Here is what i did find. I put the debug.print statement in the SocketWorker_Completed event to show the length of the string at that point. Then the exception occurs in the processStatus Method. When the data arrives in the processStatus it is empty. Based on the exception in the output window I can see that the strSize is 1025 then immediately afterwards the exception is thrown. So between testing for string size and calling the processStatus method the data is lost or it is not being passed?
0
 
BasicfarmerAuthor Commented:
Something else i just noticed. In testing for null the value of strData is not null when it comes into the processStatus method. Look at my testing code.
    Private Sub processStatus(strData As String)

        '***************************************************************
        '* Begin processing the data that was returned by the printer. *
        '***************************************************************

        If String.IsNullOrEmpty(strData) Then MsgBox("EMPTY STRING")
        'Remove any characters that might be in the string if the expected
        'data was not returned.
        strData = Replace(strData, vbCrLf, vbNullString)
        strData = Replace(strData, Chr(0), vbNullString)
        If strData = vbNullString Then MsgBox("VBNULLSTRING")
        If strData = Nothing Then MsgBox("NOTHING")
        If String.IsNullOrEmpty(strData) Then MsgBox("ISNULLOREMPTY")
        If String.IsNullOrWhiteSpace(strData) Then MsgBox("ISNULLORWHITESPACE")
        If strData.Length = 0 Then MsgBox("ZEROLENGTH")

Open in new window

The very first test String.IsNullOrEmpty(strData) then MsgBox("EMPTY STRING") is allowed.
Then I am removing carriageReturns and LineFeeds from the string
Then i am removing chr(0) from the string.
Then next test if strData = vbnullstring then msgbox("VBNULLSTRING") catches it. So in the SocketWorker_Completed event the data is only either carriage returns, line feeds, or chr(0). and so it passed all of the previous tests.

So I need to test for these things in the SocketWorker_Completed event.
0
 
BasicfarmerAuthor Commented:
This suggestion led to me finding the problem with non printable characters.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now