7

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
  • 1,041
  • 3
  • 18
  • 35

1 Answers1

9

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.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • `If IsNull(strTest)` in VB6 = `If strTest Is Nothing` in VB.net ?? Is it also right? – nnnn Oct 10 '13 at 07:44
  • 1
    +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. – Pradeep Kumar Oct 10 '13 at 07:52
  • @PradeepKumar `I've never felt the use of comparing any string to Nothing till now in vb.net yet.` Me too :) and BTW... Thanks :) – Siddharth Rout Oct 10 '13 at 07:58