Literal Keywords

Nothing, Null, Empty, True and False are keywords that stand for fixed values. They are part of the language, available in every module without a reference, and cannot be assigned to or declared again.

Keyword Value Type it belongs to Test for it with
Nothing a reference to no object an object type Is or IsNot
Null no valid data Variant IsNull
Empty nothing assigned yet Variant IsEmpty
True -1 Boolean the value itself, or CBool
False 0 Boolean the value itself, or CBool

Nothing

Nothing is the object reference that refers to no object. A variable declared As Object, or as a class or an interface, holds Nothing until an object is assigned to it. VarType of Nothing is vbObject (9), TypeName is Nothing, and ObjPtr is 0.

Nothing is assigned with Set: Set Items = Nothing. This releases the variable’s reference. Several variables can refer to one object, and the object is destroyed when the last of them is set to Nothing or goes out of scope. A property or method called through a variable that holds Nothing raises error 91, Object variable or With block variable not set.

Test for it with Is, Items Is Nothing, or with twinBASIC’s IsNot, Items IsNot Nothing. Two more forms are twinBASIC’s own: an initializer in a declaration, Dim Items As Collection = Nothing, needs no Set, and ByVal Nothing passes a null pointer for a ByRef interface parameter.

Note BETA 1010

Nothing assigned without Set to a variable that is not an object or a Variant, such as Count = Nothing for a Long, and Nothing compared with = or <>, such as If Obj = Nothing, compile and raise error 91 when they run. VB6 and 64-bit VBA refuse them when they compile, with Invalid use of object.

Object describes assigning and comparing object references.

Dim Items As Collection
Debug.Print Items Is Nothing        ' True
Debug.Print TypeName(Items)         ' Nothing
Set Items = New Collection
Debug.Print Items Is Nothing        ' False
Set Items = Nothing
Debug.Print ObjPtr(Items)           ' 0

Null

Null is a Variant value that means the variable holds no valid data, such as a field of a database record that has no value. VarType of Null is vbNull (1), and TypeName is Null. Only a Variant can hold it: assigning Null to a variable of another type raises error 94, Invalid use of Null, and Set of a Variant that holds it to an object variable raises error 424, Object required.

Null passes through most expressions. An arithmetic expression or a comparison with a Null operand is Null, so v = Null is never True: test for it with IsNull. The & operator treats Null as an empty string. Null And False is False and Null Or True is True, because the other operand decides the result; any other logical operation with Null is Null. An If condition that is Null counts as False. Many functions return Null when an argument is Null, and their $ forms raise error 94; a function’s page lists what it raises.

Dim v As Variant
v = Null
Debug.Print IsNull(v * 2)           ' True
Debug.Print IsNull(v = Null)        ' True
Debug.Print "[" & v & "]"           ' []
Debug.Print v And False             ' False
Debug.Print v Or True               ' True
If v Then
    Debug.Print "then"
Else
    Debug.Print "else"              ' else
End If

Empty

Empty is the value of a Variant that has not been assigned. VarType of Empty is vbEmpty (0), and TypeName is Empty. Assigning Empty to a Variant returns it to that state.

Empty counts as 0 in a numeric expression and as an empty string in a string expression, so Empty = 0 and Empty = "" are both True, and Empty + 1 is the Integer 1. Assigned to a variable of another type, it gives that type’s default value: 0, an empty string, or False. Test for it with IsEmpty.

Variant describes Empty and Null together with the error values a Variant can hold.

Dim v As Variant
Debug.Print IsEmpty(v)              ' True
Debug.Print v = ""                  ' True
Debug.Print TypeName(v + 1)         ' Integer
v = 5
v = Empty
Debug.Print IsEmpty(v)              ' True

True

True is the Boolean value -1, with every bit set. A comparison that holds gives True, and If, While and Do run their True branch for any number but 0. A number is not equal to True unless it is -1 itself: 5 = True is False, although CBool(5) is True. In arithmetic True counts as the Integer -1, and CStr gives True.


False

False is the Boolean value 0. It is the default value of a Boolean variable, and the result of a comparison that does not hold. In arithmetic it counts as the Integer 0, and CStr gives False.

Boolean describes how numbers and strings convert to True and False.

Debug.Print CInt(True)              ' -1
Debug.Print True + True             ' -2
Debug.Print 5 = True                ' False
Debug.Print CBool(5)                ' True
Debug.Print CStr(False)             ' False

See Also