Saturday, August 22, 2020

Understanding Delphi Class Methods

Understanding Delphi Class Methods In Delphi, a strategy is a system or capacity that plays out a procedure on an article. A class technique is a strategy that works on a class reference rather than an item reference. In the event that you set out to find the real story, you will find that class techniques are available in any event, when you havent made a case of the class (the article). Class Methods versus Item Methods Each time you make a Delphi part progressively, you utilize a class technique: the Constructor. The Create constructor is a class technique, rather than for all intents and purposes every single other strategy youll experience in Delphi programming, which are object strategies. A class strategy is a technique for the class, and properly enough, an article technique is a technique that can be called by an occurrence of the class. This is best outlined by a model, with classes and items featured in red for lucidity: myCheckbox : TCheckbox.Create(nil) ; Here, the call to Create is gone before by the class name and a period (TCheckbox.). Its a technique for the class, ordinarily known as a constructor. This is the system by which examples of a class are made. The outcome is an example of the TCheckbox class. These occasions are called objects. Complexity the past line of code with the accompanying: myCheckbox.Repaint; Here, the Repaint strategy for the TCheckbox object (acquired from TWinControl) is called. The call to Repaint is gone before by the item factor and a period (myCheckbox.). Class strategies can be called without an occasion of the class (e.g., TCheckbox.Create). Class strategies can likewise be called legitimately from an article (e.g., myCheckbox.ClassName). Anyway object strategies must be called by an occurrence of a class (e.g., myCheckbox.Repaint). In the background, the Create constructor is designating memory for the article (and playing out any extra introduction as determined by TCheckbox or its predecessors). 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 an exceptionally pleasant approach to carry out the responsibility, however just to make the code simpler to peruse (and to oversee), it would be significantly 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 system TAboutBox.ShowYourself;beginAboutBox: TAboutBox.Create(nil) ;tryAboutBox.ShowModal;finallyAboutBox.Release;end;end; Things to Keep in Mind The meaning of a class technique must incorporate the saved word class before the methodology or capacity watchword that begins the definition.AboutBox structure isn't auto-made (Project-Options).Put AboutBox unit to the utilizations provision of the primary form.Dont neglect to pronounce the strategy 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.