Monday, November 25, 2019

Understanding Delphi Class Methods

Understanding Delphi Class Methods 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 havent 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 youll 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.). Its 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) ;beginAboutBox:TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.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) ;beginTAboutBox.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;beginAboutBox: TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.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.Dont forget to declare the procedure in the interface (public) part of the AboutBox unit.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.