Name

Null Variable

Syntax

var Null: Variant;

Description

The Null variable is the Null Variant value. You should not change its value.

Tips and Tricks

  • Variant variables are initialized to Unassigned. When you want to assign a value to a Variant, but are unable to assign a specific, known value, use Null to represent an unknown or missing value. In particular, TField-derived components use the Null value to represent SQL NULL values (when you want the field value as a Variant).

  • Variant expressions that use Null as an operand produce a Null result.

  • Attempting to convert a Null value to a number raises runtime error 15 (EVariantError).

  • Refer to nil for the equivalent of NULL in C and C++.

Example

// In the Variant array Data, compute the average of all
// non-Null values.
function ComputeAverage(Data: Variant): Variant;
var
  Sum: Double;
  Count: Integer;
  I: Integer;
begin
  Sum := 0.0;
  Count := 0;
  for I := VarArrayLowBound(Data) to VarArrayHighBound(Data) do
    if not VarIsNull(Data[I]) then
    begin
      Sum := Sum + Data[I];
      Inc(Count);
    end;
 if Count = 0 then
    Result := Null
  else
    Result := Sum / Count;
end;

See Also

EmptyParam Variable, Unassigned Variable, Variant Type, VarIsNull Function

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.