Recent

Author Topic: self  (Read 357 times)

smitty

  • New Member
  • *
  • Posts: 13
self
« on: September 11, 2021, 11:31:29 pm »
Reading https://wiki.freepascal.org/Self
Code: [Select]
Self is a keyword which can be used in instance methods to refer to the object on which the currently
executing method has been invoked.
Reserved word self used to represent an instance of the class in which it appears.
Self can be used to access class members and as a reference to the current instance.


procedure TForm1.FormCreate(Sender: TObject);
begin
  // Self stands for the TForm1 class in this example
  Self.Caption := 'Test program';
  Self.Visible := True;
end;

Therefore, presume I create my own Class named Math and I create methods add, subtract, divide, multiply.  If I create a new procedure
Code: Pascal  [Select][+][-]
  1. procedure Math.FormCreate();
  2. Self.Add := a;


I know that is a horrible example, but essentially Self means "if I am an instance of Class Math and want to use a method of Class Math, then I use the syntax 'Self.Method'" where Method must be in the Class.

Thanks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9587
  • FPC developer.
Re: self
« Reply #1 on: September 11, 2021, 11:40:15 pm »
Yeah, but keep in mind that the methods of the own class are visible by default.

So self is mostly for special cases like when parameter names clash, within WITH statements etc.

 

TinyPortal © 2005-2018