RAD Studio VCL Reference
Variants.Null Function

Returns a Null variant.

function Null: Variant;
Variant Null();

Use Null to obtain a Null variant that can indicate unknown or missing data. Null Variants can be assigned to variant variables in an application that must contain a null value. Assigning Null to a variant variable does not cause an error, and Null can be returned from any function with a variant return value. 

Assigning Null to a variable of any type other than System::Variant causes either a compile-time error or raises an EVariantTypeMismatch exception. For example, in the following code the assignment of v, the Null variant, to variant q is successful; whereas the conversion of variant v, which is now Null, to the integer return type of the Test function, raises an exception. 

function Test(v: System::Variant): Integer;

  q: Variant;
  msg: string;
  q := v; { this is ok, since q is a variant }
  if VarIsNull(q) then
    msg := 'q is a null variant'
    msg := 'q is not a null variant';
  Result := v; { this raises an exception!!!}


procedure TForm1.Button1Click(Sender: TObject);



int Test(Variant v)

  Variant q = v; // this is ok, since q is a variant
  AnsiString msg;
  if (VarIsNull(q))
    msg = "q is a null variant";
    msg = "q is not a null variant";
   return v; // this throws an exception!!!

void __fastcall TForm1::Button1Click(TObject *Sender) 



Except for comparisons, which always result in Boolean values, expressions involving Null variants always result in Null variants.

Note: The Null variant returned by the Null function is different from the nil constant in Delphi code and the NULL macro in C++ code. Neither nil nor NULL is a variant. The nil constant is an object reference that does not refer to any actual object. The NULL macro is a pointer that does not refer to any actual object or value.


Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!