Name
Self Variable
Syntax
var Self: Class type;Description
In every
method, Delphi declares the Self variable as a
hidden parameter. In a method, the value of the
Self variable is the object reference. In a class
method, Self is the class reference.
Tips and Tricks
A constructor can assign a new value to
Self, which becomes the value the constructor returns. Usually, though, you should override theNewInstancemethod instead of assigning toSelf.In ordinary methods, assigning to
Selfhas no effect outside the method.A method also has an implicit
withSelfdofor the method’s body. In other words, all of the fields, methods, and properties are in scope, and you can refer to them without the explicit reference toSelf.In the
registercalling convention,Selfis the first argument, so it is passed in theEAXregister.In the
pascalcalling convention,Selfis the last argument, so it is pushed last onto the stack, after all other arguments.In the
cdecl,safecall, andstdcallcalling conventions,Selfis the last argument to a procedure, so it is pushed first onto the stack. If a function must return a string, dynamic array,Variantor large record result, a pointer to the result is pushed afterSelf, as a hiddenvarparameter.
Example
procedure TForm1.FormCreate(Sender: TObject); var Button: TButton; begin Button := TButton.Create(Self); Button.Parent := Self; // Center the button on the form. Note that Self.ClientWidth // and plain ClientWidth are the same property. Button.Left := (Self.ClientWidth ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access