Methods


In component development, you will mostly be creating public and protected methods — public methods for everyday use and protected methods for use in descendant classes.

At this point in the book, it shouldn't be necessary to tell you how to create a new method, but for the sake of completeness, the following listing shows the TSimple class with a simple SayHello method. Note that the SayHello method was created as an example of a simple method, not one that is very optimized.

Listing 24-6: A simple method

image from book
unit Simple; interface uses   SysUtils, Classes, Dialogs; type   TDaysEnum = (deMonday, deTuesday, deWednesday, deThursday,     deFriday, deSaturday, deSunday);   TDays = set of TDaysEnum;   TSimple = class(TComponent)   private     { Private declarations }     FEnumeration: TDaysEnum;     FBool: Boolean;     FWorkDays: TDays;   protected     { Protected declarations }   public     { Public declarations }     procedure SayHello;   published     { Published declarations }     property Bool: Boolean read FBool write FBool default False;     property Enumeration: TDaysEnum       read FEnumeration write FEnumeration default deMonday;     property WorkDays: TDays read FWorkDays write FWorkDays;   end; procedure Register; implementation procedure Register; begin   RegisterComponents('My Components', [TSimple]); end; procedure TSimple.SayHello; var   msg: string;   cnt: Integer;   day: TDaysEnum; const   NAMES: array[TDaysEnum] of string = ('Monday', 'Tuesday',     'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'); begin   if WorkDays = [] then     msg := 'I don''t have to work this week.'   else begin     msg := 'I have to work on ';     cnt := 0;     for day in WorkDays do     begin       Inc(cnt);       if cnt > 1 then         msg := msg + ', ' + NAMES[day]       else         msg := msg + NAMES[day];     end;      // for     msg := msg + '.';   end;        // if WorkDays   { add Dialogs to the uses list to use MessageDlg }   MessageDlg(msg, mtInformation, [mbOK], 0); end; end.
image from book

To see what the SayHello method does, take a look at Figure 24-13.

image from book
Figure 24-13: The output of the SayHello method

The SayHello method shown in Listing 24-6 is a static method. Static methods are methods not marked with either the virtual or the dynamic directive. The primary advantage of static methods is that they are faster than other method types. The primary disadvantage of static methods is that they cannot be overridden in descendant classes.

Unlike static methods, both virtual and dynamic methods can be overridden using the override directive. Overriding gives us the ability to change a method in a descendant class. It takes a bit longer to execute virtual and dynamic methods because the appropriate method must be determined at run time. When you have to create a non-static method, it is usually best to create a virtual method rather than a dynamic method, because they are faster than dynamic methods.

The following listing shows three different classes that illustrate how to create a virtual method and how to override it in descendant classes.

Listing 24-7: Overriding virtual methods

image from book
unit Animals; interface uses Dialogs; type   TAnimal = class   public     procedure Hello; virtual;   end;   TDog = class(TAnimal)     procedure Hello; override;   end;   TCat = class(TAnimal)     procedure Hello; override;   end; implementation procedure TAnimal.Hello; begin end; procedure TDog.Hello; begin   ShowMessage('Class = ' +  ClassName); end; procedure TCat.Hello; begin   MessageDlg('Does the word Polymorphism ring a bell?',     mtInformation, [mbOK], 0); end; end.
image from book

To see how virtual methods work, write the code displayed in Listing 24-8 and see what happens. The result of calling the virtual method Hello is displayed in Figure 24-14.

image from book
Figure 24-14: Calling the virtual method Hello

Listing 24-8: Calling a virtual method

image from book
procedure TForm1.Button1Click(Sender: TObject); var   A: TAnimal; begin   A := TCat.Create;   A.Hello; { calls TCat.Hello }   A.Free; end;
image from book

After this short escapade, it's time to return to our TSimple component. The last thing that you should learn in this section is how to properly set default property values. To set, for instance, the deWednesday value as the default value for the Enumeration property, you have to mark the Enumeration property with the default directive, override the component's constructor, and assign the same value to the Enumeration property in the constructor. The following listing shows how this is done. Note that you must always call the inherited constructor first and only then can you do anything else in the constructor.

Listing 24-9: Properly defining default property values

image from book
unit Simple; interface uses   SysUtils, Classes, Dialogs; type   TDaysEnum = (deMonday, deTuesday, deWednesday, deThursday,     deFriday, deSaturday, deSunday);   TDays = set of TDaysEnum;   TSimple = class(TComponent)   private     { Private declarations }     FEnumeration: TDaysEnum;     FBool: Boolean;     FWorkDays: TDays;   protected     { Protected declarations }   public     { Public declarations }     constructor Create(AOwner: TComponent); override;   published     { Published declarations }     property Bool: Boolean read FBool write FBool;     property Enumeration: TDaysEnum       read FEnumeration write FEnumeration default deWednesday;     property WorkDays: TDays read FWorkDays write FWorkDays;   end; procedure Register; implementation procedure Register; begin   RegisterComponents('My Components', [TSimple]); end; constructor TSimple.Create(AOwner: TComponent); begin   inherited Create(AOwner); { you must call the inherited constructor first! }   FEnumeration := deWednesday; end; end.
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net