IsNull in VB6 and VB.net

20,763

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.

Share:
20,763
nnnn
Author by

nnnn

I am Programmer. I am Buddhist,I love art &amp; my friend. My super power is sleeping. =)

Updated on July 09, 2022

Comments

  • nnnn
    nnnn almost 2 years

    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.

  • nnnn
    nnnn over 10 years
    If IsNull(strTest) in VB6 = If strTest Is Nothing in VB.net ?? Is it also right?
  • Pradeep Kumar
    Pradeep Kumar over 10 years
    +1 Sid. Nicely explained. @nnnn: Yes that's right. The IsNull check can be compared with Nothing in VB.NET. But be aware that strings behave a bit differently in VB6 and VB.NET. I always use the IsNullOrEmpty check or the IsNullOrWhiteSpace check (as the case may be), rather than a direct "" string compare or Nothing or String.Empty. VB.NET internally does a lot of things to make these three things seem similar. I've never felt the use of comparing any string to Nothing till now in vb.net yet.
  • Siddharth Rout
    Siddharth Rout over 10 years
    @PradeepKumar I've never felt the use of comparing any string to Nothing till now in vb.net yet. Me too :) and BTW... Thanks :)