0

I am using 3rd party library to obtain data from a machine's variable. The data returns a string type, so after requesting data I'm validating that it is not an empty string like that:

Not String.IsNullOrWhiteSpace(data) 

However, my program was failing, and I realized that when the variable in the machine was empty, the function above was returning True.

By debugging I noticed that the function in the 3rd party library returns vbNullChar when the variable is empty and String.IsNullOrWhiteSpace(vbNullChar) evaluates to False, when I would expect it to evaluate to True as String.IsNullOrWhiteSpace(vbNullString) does.

So is my only option to check as follows?

Not String.IsNullOrWhiteSpace(data) AndAlso data <> vbNullChar 
1
  • 1
    Because it is neither a null reference nor an empty string? It is a string that contains 1 character, the \0 code. Commented Feb 28, 2019 at 8:54

1 Answer 1

5

Because the two things you want to compare here are different "null" things.

String.IsNullOrWhiteSpace returns true if:

  • The reference being passed to the method is null
  • or ... every character of the non-null string is categorized as a Whitespace character ("every character" is true if the string has a length of 0)

In this case you're trying to pass in Constants.vbNullChar which is neither, it is a string which consists of 1 character, the \0 character, which is not categorized as Whitespace.

Constants.vbNullString, on the other hand, is a null-reference, which String.IsNullOrWhiteSpace handles just fine.


As for how you should handle this concrete problem, I would do a replacement from vbNullChar to null (vbNullString) immediately after obtaining the results, so that this oddity will have to checked only one place.

Additionally, if you get back a string, does it end with this \0 character? If so then you probably want to remove that as well.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.