Search code examples
stringvb6

VB6 equivalent of string.IsNullOrEmpty


I'm doing some work on a legacy application, and my VB6 skills aren't that great. I need to check whether a String field has been initialized and set to something other than null/nothing or an empty string. In C# I'd just do something like:

if (string.IsNullOrEmpty(myObj.Str))

I'm not sure what the equivalent to this was in VB6, and I'm nervous about using If myObj.Str = "" and calling it good. What's the correct way to do this?

To clarify, I want something that will return True if any of the following are true:

  • The field hasn't been initialized
  • The field is an empty string (str = "", length = 0)
  • The field is set to null, or Nothing, or vbnull, or whatever form of the null value applies to VB6 strings.

The field was originally a Long, and the code I'm replacing checked whether it was set to 0.


Solution

  • VB6 was designed to be easy

    Use

    If str = "" Then 
      ' uninitialised, null or empty ""
    
    • Strings are automatically initialized to [edit] a null string.
    • The null string is vbNullString.
    • But don't worry about null strings. A VB6 null string is indistinguishable from an empty string "" for (almost) any string manipulation.