DelphiBasics
Null
Variable
A variable that has no value Variants unit
  var Null : Variant;
Description
The Null variable has an undefined value.
 
Null does not mean a zero number or empty string - it is undefined. Any expression using a null variable will yield a null result.
 
Null is particularly useful when handling SQL table data - providing a one for one correspondence with the SQL NULL value.
Notes
The Null variable is the Null Variant.

Calculations using nulls yield the EVariantError.
Related commands
Pointer Defines a general use Pointer to any memory based data
Variant A variable type that can hold changing data types
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Using Null to represent the result of a bad divide
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  Variants,   // Unit containing the Null command
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
     function Divide(Dividend, Divisor: Integer) : Variant;
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  Answer : Variant;

begin
  Answer := Divide(4,2);

  // Show the result of this division
  if Answer = Null
  then ShowMessage('4 / 2 = Invalid')
  else ShowMessage('4 / 2 = '+IntToStr(Answer));

  Answer := Divide(4,0);

  // Show the result of this division
  if Answer = Null
  then ShowMessage('4 / 0 = Invalid')
  else ShowMessage('4 / 0 = '+IntToStr(Answer));

end;

function TForm1.Divide(Dividend, Divisor: Integer) : Variant;
begin
  // Try to divide the Dividend by the Divisor
  try
    Result := Dividend div Divisor;
  except
    Result := Null ;    // Assign Null if the division threw an error
  end;
end;
 
end.
Hide full unit code
   4 / 2 = 2
   4 / 0 = Invalid
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page