Delphi Basics
Nil
Constant
A pointer value that is defined as undetermined System unit
  const Nil = Pointer(0);
Description
The Nil constant is a pointer value that is defined as undetermined.
 
Use of a Nil pointer will result in an exception.
 
Nil is mostly used as a substitute for a Pointer parameter - it tells the routine that no Pointer value is available for this parameter.
 
Pointer variables are not set to Nil except in special circumstances, such as when creating a new object that contains pointers. This is because Delphi initialises the storage taken by a new object to 0's. A Nil pointer is one that has the value 0.
Related commands
Assigned Returns true if a reference is not nil
Null A variable that has no value
Pointer Defines a general use Pointer to any memory based data
 
Example code : A simple example
// 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
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myPtr : PChar;

begin
  // Pointer variables are not set to nil by default
  if myPtr = Nil
  then ShowMessage('myPtr is nil')
  else ShowMessage('myPtr is not nil');

  // So we must set them to nil to be sure that they are undefined
  myPtr := Nil;
  if myPtr = Nil
  then ShowMessage('myPtr is nil')
  else ShowMessage('myPtr is still not nil');
end;
 
end.
Hide full unit code
   myPtr is not nil
   myPtr is nil
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page