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:
Add the object field to the component.
Define the object property.
Create the object in the component's constructor.
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 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
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.
The following figure shows the TStringsCache component on the Designer Surface.
Figure 24-19: The TStringsCache component