Inside Delphi 2006 (Wordware Delphi Developers Library)
Properties with direct access are the easiest to write and, of course, slightly faster than properties that use methods to read values from and write values to underlying fields (if there are such). The downside of direct-access properties is that they are less flexible than properties that use read and/or write methods, since they have no control over what's happening with the prop- erty's value.
To illustrate how to create a write method, let's create a new integer property named Limited that will only allow the user to enter numbers from 0 to 100. Since the Limited property is an integer property, the write method must be a procedure that accepts a single integer parameter. To enable yourself, or other developers, to update the Limited property in the future, you should create the write method in the protected section of the class and mark it with the virtual directive:
protected { Protected declarations } procedure SetLimited(Value: Integer); virtual; published { Published declarations } { use the SetLimited method to write values to the Limited property } property Limited: Integer read FLimited write SetLimited; end;
The following listing shows the entire TSimple class with the Limited property. All other properties were removed so that you can focus on the code related to the Limited property.
Listing 24-10: The Limited property's write method
unit Simple; interface uses SysUtils, Classes, Dialogs; type TSimple = class(TComponent) private { Private declarations } FLimited: Integer; protected { Protected declarations } procedure SetLimited(Value: Integer); virtual; published { Published declarations } property Limited: Integer read FLimited write SetLimited; end; procedure Register; implementation procedure Register; begin RegisterComponents('My Components', [TSimple]); end; procedure TSimple.SetLimited(Value: Integer); begin if (Value >= 0) and (Value <= 100) then FLimited := Value else raise ERangeError.Create('Give me a number from 0 to 100!'); end; end.
The SetLimited method is now implicitly called every time anyone tries to change the value of the Limited property. Figure 24-15 shows what happens when you try to assign an invalid value in the Object Inspector.
Just as you can create the write method to control what happens when someone tries to write a new value to the property, you can create a read method to control what happens when someone reads the existing value of the property. Read methods have to be parameterless functions whose result type matches that of the property.
To illustrate how to create a read method, let's create a new property called AccessCount that will be updated by the read method of the Limited property every time someone reads its value. The AccessCount property can be both public and read-only. It can be public because its value only changes at run time and it can be read-only because its value is automatically updated by the Limited property. To create a read-only property, you only have to omit the write directive.
Listing 24-11 shows the TSimple component with the new AccessCount property and the updated Limited property.
unit Simple; interface uses SysUtils, Classes, Dialogs; type TSimple = class(TComponent) private { Private declarations } FLimited: Integer; FAccessCount: Integer; protected { Protected declarations } function GetLimited: Integer; virtual; procedure SetLimited(Value: Integer); virtual; public property AccessCount: Integer read FAccessCount; { read-only } published { Published declarations } property Limited: Integer read GetLimited write SetLimited; end; procedure Register; implementation procedure Register; begin RegisterComponents('My Components', [TSimple]); end; procedure TSimple.SetLimited(Value: Integer); begin if (Value >= 0) and (Value <= 100) then FLimited := Value else raise ERangeError.Create('Give me a number from 0 to 100!'); end; function TSimple.GetLimited: Integer; begin Inc(FAccessCount); Result := FLimited; end; end.
The following figure shows the value of the AccessCount property after accessing the Limited property several times.