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
BasicfarmerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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 SotoRetiredCommented:
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 SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.