Object Properties


Although it requires a bit more work, you can also add object properties to your components. To create an object property, you have to do the following:

  1. Add the object field to the component.

  2. Define the object property.

  3. Create the object in the component's constructor.

  4. Destroy the object in the component's destructor.

Now we are going to create a new component called TStringsCache. The TStringsCache component will be useful not only because it shows how to create an object property, but also because it will enable you to easily add strings or even entire text files to the executable.

First, you have to create the TStringsCache component using the New VCL Component dialog box. To create the TStringsCache component, select the TComponent class as the ancestor class, save the component's unit, preferably image from book StringsCache.pas, in the same directory where you have previously saved the TSimple component, and finally add the TStringsCache component to your package.

When the wizard is finished, you can start creating the component by adding the Strings property to the interface part of the class. Note that when you're creating subcomponents you must use a write method for the property because you aren't changing the object, just its properties:

type   TStringsCache = class(TComponent)   private     { Private declarations }     FStrings: TStrings;   protected     { Protected declarations }     procedure SetStrings(Value: TStrings); virtual;   public     { Public declarations }   published     { Published declarations }     property Strings: TStrings read FStrings write SetStrings;   end;

The TStringsCache component is actually almost finished. To finish it, you have to override the component's destructor and constructor to create and destroy the FStrings object, and you have to write the implementation of the SetStrings method, which only calls Assign to copy the properties of the Value parameter to the FStrings object. Also note that in order to create the FStrings object, you have to create an instance of the TStringList class, not the TStrings class, because the TStrings class cannot be directly instantiated.

The following listing shows the entire TStringsCache component.

Listing 24-13: The TStringsCache component

image from book
unit StringsCache; interface uses   SysUtils, Classes; type   TStringsCache = class(TComponent)   private     { Private declarations }     FStrings: TStrings;   protected     { Protected declarations }     procedure SetStrings(Value: TStrings); virtual;   public     { Public declarations }     constructor Create(AOwner: TComponent); override;     destructor Destroy; override;   published     { Published declarations }     property Strings: TStrings read FStrings write SetStrings;   end; procedure Register; implementation procedure Register; begin   RegisterComponents('My Components', [TStringsCache]); end; constructor TStringsCache.Create(AOwner: TComponent); begin   inherited Create(AOwner);   FStrings := TStringList.Create; end; destructor TStringsCache.Destroy; begin   FStrings.Free;   { always call the inherited destructor, and always call it last }   inherited Destroy; end; procedure TStringsCache.SetStrings(Value: TStrings); begin   FStrings.Assign(Value); end; end.
image from book

The following figure shows the TStringsCache component on the Designer Surface.

image from book
Figure 24-19: The TStringsCache component



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