Saturday, August 22, 2020

Understanding Delphi Class Methods

Understanding Delphi Class Methods In Delphi, a technique is a strategy or capacity that plays out a procedure on an item. A class strategy is a technique that works on a class reference rather than an item reference. On the off chance that you set out to find the real story, you will find that class strategies are open in any event, when you havent made an occasion of the class (the article). Class Methods versus Item Methods Each time you make a Delphi part powerfully, you utilize a class strategy: the Constructor. The Create constructor is a class strategy, rather than essentially all different techniques youll experience in Delphi programming, which are object strategies. A class technique is a strategy for the class, and properly enough, an item technique is a technique that can be called by an occurrence of the class. This is best represented by a model, with classes and articles featured in red for clearness: myCheckbox : TCheckbox.Create(nil) ; Here, the call to Create is gone before by the class name and a period (TCheckbox.). Its a strategy for the class, regularly known as a constructor. This is the instrument by which occasions of a class are made. The outcome is an occasion of the TCheckbox class. These examples are called objects. Difference the past line of code with the accompanying: myCheckbox.Repaint; Here, the Repaint technique for the TCheckbox object (acquired from TWinControl) is called. The call to Repaint is gone before by the article variable and a period (myCheckbox.). Class techniques can be called without a case of the class (e.g., TCheckbox.Create). Class strategies can likewise be called straightforwardly from an article (e.g., myCheckbox.ClassName). Anyway object strategies must be called by a case of a class (e.g., myCheckbox.Repaint). In the background, the Create constructor is dispensing memory for the article (and playing out any extra instatement as indicated by TCheckbox or its precursors). Exploring different avenues regarding Your Own Class Methods Consider AboutBox (a custom About This Application structure). The accompanying code utilizes something like:procedure TfrMain.mnuInfoClick(Sender: TObject) ;beginAboutBox:TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.Release;end;end;This, obviously, is a decent method to carry out the responsibility, however just to make the code simpler to peruse (and to oversee), it would be substantially more proficient to transform it to:procedure TfrMain.mnuInfoClick(Sender: TObject) ;beginTAboutBox.ShowYourself;end;The above line calls the ShowYourself class technique for the TAboutBox class. The ShowYourself must be set apart with the watchword class:class method TAboutBox.ShowYourself;beginAboutBox: TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.Release;end;end; Things to Keep in Mind The meaning of a class strategy must incorporate the saved word class before the method or capacity catchphrase that begins the definition.AboutBox structure isn't auto-made (Project-Options).Put AboutBox unit to the utilizations statement of the principle form.Dont neglect to pronounce the technique in the interface (open) some portion of the AboutBox unit.

No comments:

Post a Comment

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