Skip to main content
Version: v20 R2 BETA

Null and Undefined

Null and Undefined are data types that handle cases where the value of an expression is not known.

Null

Null is a special data type with only one possible value: null. This value is returned by an expression that does not contain any value.

In the 4D language and for object field attributes, null values are managed through the Null function. This function can be used with the following expressions for setting or comparing the null value:

  • object attributes
  • collection elements
  • variables of the object, collection, pointer, picture, or variant type.

Undefined

Undefined is not actually a data type. It denotes a variable that has not yet been defined. A function (a project method that returns a result) can return an undefined value if, within the method, the function result ($0) is assigned an undefined expression (an expression calculated with at least one undefined variable). A field cannot be undefined (the Undefined command always returns False for a field). A variant variable has undefined as default value.

Null operators

OperationSyntaxReturnsExpressionValue
EqualityNull = NullBooleana.nullProp = b.nullPropTrue
Null = UndefinedBooleana.nullProp = b.undefinedPropTrue
Null = scalar valueBooleana.nullProp = 42False
InequalityNull # NullBooleana.nullProp # b.nullPropFalse
Null # UndefinedBooleana.nullProp # b.undefinedPropFalse
Null # scalar valueBooleana.nullProp # 42True

scalar values are values of type string, Date, Time, Boolean, number, or Blob. When declared, their default value is neither undefined nor null. Other types (Pointer, Picture, Object, Collection) have undefined or null default value. Ex:

var $object : Object
var $text : Text

//$object = null
//$text = ""
info

Comparisons with Greater than (>), Less than (<), Greater than or equal to (>=), and Less than or equal to (<=) operators are not supported with Null values and return an error.

Undefined operators

OperationSyntaxReturnsExpressionValue
EqualityUndefined = UndefinedBooleana.undefinedProp = b.undefinedPropTrue
Undefined = NullBooleana.undefinedProp = c.nullPropTrue
Undefined = other valuesBooleana.undefinedProp = 42False
InequalityUndefined # UndefinedBooleana.undefinedProp # b.undefinedPropFalse
Undefined # NullBooleana.undefinedProp # b.nullPropFalse
Undefined # other valuesBooleana.undefinedProp # 42True
Greater thanUndefined > string, Date, Time, Boolean, numberBooleana.undefinedProp > "abc"False
Less thanUndefined < string, Date, Time, Boolean, numberBooleana.undefinedProp < "abc"False
Greater than or equal toUndefined >= string, Date, Time, Boolean, numberBooleana.undefinedProp >= "abc"False
Less than or equal toUndefined <= string, Date, Time, Boolean, numberBooleana.undefinedProp <= "abc"False

other values are expressions of any type with a value neither Undefined nor Null.

info

Comparisons of Undefined values with Pointer, Picture, Blob, Object, Collection, Undefined or Null values using Greater than (>), Less than (<), Greater than or equal to (>=), and Less than or equal to (<=) operators are not supported and return an error.

Examples

Here are the different results of the Undefined command as well as the Null command with object properties, depending on the context:

var $vEmp : Object
var $result : Boolean
$vEmp:=New object
$vEmp.name:="Smith"

$vEmp.children:=Null

$result:=Undefined($vEmp.name) //False
$result:=($vEmp.name=Null) //False

$result:=Undefined($vEmp.children) //False
$result:=($vEmp.children=Null) //True

$result:=Undefined($vEmp.parent) //True
$result:=($vEmp.parent=Null) //True

Examples of comparison results with undefined and null values:

var $result : Boolean
var $vObj : Object
var $vVar : Variant

$vObj:=New object
$vObj.null:=Null
//note that $vVar is not assigned

$result:=($vObj.undefined=42) //False
$result:=($vObj.undefined=$vObj.null) //True
$result:=($vObj.undefined=$vVar) //True

$result:=($vObj.undefined#$vObj.null) //False
$result:=($vObj.undefined#42) //True
$result:=($vObj.undefined#$vVar) //False

$result:=($vObj.undefined>"hello") //False
$result:=($vObj.undefined>$vVar) //Error
$result:=($vObj.undefined>$vObj.null) //Error
$result:=($vVar < 42) //False