IsNull in VB6 and VB.net

nnnn picture nnnn · Oct 10, 2013 · Viewed 19.9k times · Source

I have a code -

strTest="    "    
IsNull(Trim(strTest)) 

It returns False in VB6 .

I write this code to VB.net but

IsNull(Trim(strTest))

returns True .
So, IsNull(Trim(" ")) in VB6 = ?? in VB.net
Thank you.

Answer

Siddharth Rout picture Siddharth Rout · Oct 10, 2013

There is no IsNull function in VB.Net. Instead it has other things like String.IsNullOrEmpty function and String.Empty property etc. for finding out whether a string is empty or not.

IsNull in VB6/VBA means whether an expression contains no valid data. You are getting False in vb6 because you have initialized strTest. It holds an empty string. You might also want to see THIS

VB6

IsNull(Trim(strTest)) 

In VB.Net, IsNullOrEmpty Indicates whether the specified string is Nothing or an Empty string.

VB.NET

If String.IsNullOrEmpty(strTest.Trim) Then DoWhatever
If strTest.Trim = String.Empty Then DoWhatever
If strTest.Trim = "" Then DoWhatever      '<~~ Same in VB6 as well
If String.IsNullOrWhiteSpace(strTest) Then DoWhatever  '<~~ VB2010 onwards only

All these will return True in VB.Net because the string IS EMPTY. You might want to see THIS

If your string value is all spaces then either use strTest.Trim() before using the first 3 options, or use the 4th option directly which checks whether it is nothing, or empty string or all spaces only.