Understanding Delphi Class Methods

Young man using his laptop to try to solve problem with code
Getty Images/Emilija Manevska

In Delphi, a method is a procedure or function that performs an operation on an object. A class method is a method that operates on a class reference instead of an object reference.

If you read between the lines, you will find that class methods are accessible even when you haven't created an instance of the class (the object).

Class Methods vs. Object Methods

Every time you create a Delphi component dynamically, you use a class method: the Constructor.

The Create constructor is a class method, as opposed to virtually all other methods you'll encounter in Delphi programming, which are object methods. A class method is a method of the class, and appropriately enough, an object method is a method that can be called by an instance of the class. This is best illustrated by an example, with classes and objects highlighted in red for clarity:

myCheckbox := TCheckbox.Create(nil) ;

Here, the call to Create is preceded by the class name and a period ("TCheckbox."). It's a method of the class, commonly known as a constructor. This is the mechanism by which instances of a class are created. The result is an instance of the TCheckbox class. These instances are called objects. Contrast the previous line of code with the following:

myCheckbox.Repaint;

Here, the Repaint method of the TCheckbox object (inherited from TWinControl) is called. The call to Repaint is preceded by the object variable and a period ("myCheckbox.").

Class methods can be called without an instance of the class (e.g., "TCheckbox.Create"). Class methods can also be called directly from an object (e.g., "myCheckbox.ClassName"). However object methods can only be called by an instance of a class (e.g., "myCheckbox.Repaint").

Behind the scenes, the Create constructor is allocating memory for the object (and performing any additional initialization as specified by TCheckbox or its ancestors).

Experimenting With Your Own Class Methods

Think of AboutBox (a custom "About This Application" form). The following code uses something like:

procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
AboutBox:=TAboutBox.Create(nil) ;
try
AboutBox.ShowModal;
finally
AboutBox.Release;
end;
end;
This, of course, is a very nice way to do the job, but just to make the code easier to read (and to manage), it would be much more efficient to change it to:
procedure TfrMain.mnuInfoClick(Sender: TObject) ;
begin
TAboutBox.ShowYourself;
end;
The above line calls the "ShowYourself" class method of the TAboutBox class. The "ShowYourself" must be marked with the keyword "class":
class procedure TAboutBox.ShowYourself;
begin
AboutBox:= TAboutBox.Create(nil) ;
try
AboutBox.ShowModal;
finally
AboutBox.Release;
end;
end;

Things to Keep in Mind

  • The definition of a class method must include the reserved word class before the procedure or function keyword that starts the definition.
  • AboutBox form is not auto-created (Project-Options).
  • Put AboutBox unit to the uses clause of the main form.
  • Don't forget to declare the procedure in the interface (public) part of the AboutBox unit.
Format
mla apa chicago
Your Citation
Gajic, Zarko. "Understanding Delphi Class Methods." ThoughtCo, Aug. 27, 2020, thoughtco.com/understanding-class-methods-1058182. Gajic, Zarko. (2020, August 27). Understanding Delphi Class Methods. Retrieved from https://www.thoughtco.com/understanding-class-methods-1058182 Gajic, Zarko. "Understanding Delphi Class Methods." ThoughtCo. https://www.thoughtco.com/understanding-class-methods-1058182 (accessed March 29, 2024).