first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

The Differences among Empty, Nothing, vbNull, vbNullChar, vbNullString and the Zero-Length String

Back in the days of VB 5.0, Keith Pleas in "Avoid Programming Pitfalls" (currently at http://msdn.microsoft.com/library/periodic/period98/kp0398.htm) writes:

[begin quote]

To keep from being embarrassed, you should know these terms:

"": A zero-length string (commonly called an "empty string") is technically a zero-length BSTR that actually uses six bytes of memory. In general, you should use the constant vbNullString instead, particularly when calling external DLL procedures.

Empty: A variant of VarType 0 (vbEmpty) that has not yet been initialized. Test whether it is "nil" using the IsEmpty function.

Nothing: Destroys an object reference using the Set statement. Test whether it is "nil" using the Is operator:

If obj Is Nothing Then...

Null: A variant of VarType 1 (vbNull) that means "no valid data" and generally indicates a database field with no value. Don't confuse this with a C NULL, which indicates zero. Test whether it is "nil" using the IsNull function.

vbNullChar: A character having a value of zero. It is commonly used for adding a C NULL to a string or for filling a fixed-length string with zeroes:

Path = String(255, vbNullChar)

vbNullString: A string having a value of zero, such as a C NULL, that takes no memory. Use this string for calling external procedures looking for a null pointer to a string. To distinguish between vbNullString and "", use the VBA StrPtr function: StrPtr(vbNullString) is zero, while StrPtr("") is a nonzero memory address. [Note: the StrPtr() function may not be a part of the VBA members shown in the Object Browser so it will not AutoComplete. Enter the following line in the Immediate Window to determine if this function is available: ?StrPtr(vbNullString). This should return 0 immediately.]

[end quote]

From the above I draw the following conclusions: The Zero-length string is old-fashioned and used for backward compatibility with previous versions of VB; use of vbNullString is recommended. The Empty string should be used in statements with Variants. The Nothing keyword should be used in statements with object variables. The vbNull constant is used to determine if a Variant contains Null via the VarType() function; this is used in procedures involving database result sets. The vbNullChar and vbNullString constants are primarily for communicating with APIs written in C or C++.

For more information please see MSDN library article "The meaning of zero" (http://msdn.microsoft.com/library/books/advnvb5/Ch07_31.htm).

mod date: 2000-03-02T03:31:45.000Z