Quick start manual

Classes and objects
7-19
Properties
When a property is referenced in an expression, its value is read using the field or
method listed in the read specifier. When a property is referenced in an assignment
statement, its value is written using the field or method listed in the write specifier.
The example below declares a class called TCompass with a published property called
Heading. The value of Heading is read through the FHeading field and written through
the SetHeading procedure.
type
THeading = 0..359;
TCompass = class(TControl)
private
FHeading: THeading;
procedure SetHeading(Value: THeading);
published
property Heading: THeading read FHeading write SetHeading;
ƒ
end;
Given this declaration, the statements
if Compass.Heading = 180 then GoingSouth;
Compass.Heading := 135;
correspond to
if Compass.FHeading = 180 then GoingSouth;
Compass.SetHeading(135);
In the TCompass class, no action is associated with reading the Heading property; the
read operation consists of retrieving the value stored in the FHeading field. On the
other hand, assigning a value to the Heading property translates into a call to the
SetHeading method, which, presumably, stores the new value in the FHeading field as
well as performing other actions. For example, SetHeading might be implemented like
this:
procedure TCompass.SetHeading(Value: THeading);
begin
if FHeading <> Value then
begin
FHeading := Value;
Repaint; // update user interface to reflect new value
end;
end;
A property whose declaration includes only a read specifier is a read-only property,
and one whose declaration includes only a write specifier is a write-only property. It
is an error to assign a value to a read-only property or use a write-only property in an
expression.