Mega Code Archive

 
Categories / Delphi / Variables
 

Self - hidden parameter to a method - refers to the containing object

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 As Used for casting object references Class Starts the declaration of a type of object class Function Defines a subroutine that returns a value Is Tests whether an object is a certain class or ascendant Procedure Defines a subroutine that does not return a value 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