DelphiBasics
Self
Variable
Hidden parameter to a method - refers to the containing object System unit
var Self : Class Type;
Description
The Self variable is hidden parameter to every method in an object. It allows the method to refer to the object it is a method of.
 
This is particularly important when method parameters are the same as class variables intended to hold these passed values.
Related commands
AsUsed for casting object references
ClassStarts the declaration of a type of object class
FunctionDefines a subroutine that returns a value
IsTests whether an object is a certain class or ascendant
ProcedureDefines a subroutine that does not return a value
 Download this web site as a Windows program.




 
Example code : Set some properties of the main program form
// 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
  Forms, Dialogs, Classes, Controls, StdCtrls, Windows;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Use Self to identify the form to allow us to set
  // some of the form attributes
  Self.Caption := 'Test program';
  Self.Visible := True;
end;

end.
  The form is displayed with caption:
  
  Test Program
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page