Specifying a property
Method-based property mapping is specified as follows:...When a program assigns a value to a property, this value is transferred as the AData parameter to the Set procedure (method). The Set method can process this parameter in various ways before assigning a value to the FData variable. FData has private scope to ensure that access to the value of FData is strictly controlled.
TThisClass = class (TObject)
private
FData: type;
procedure SetData (const AData: type) ;
function GetData: type;
public
property Data: type read GetData write SetData;
...
function TThisClass.GetData: type;
begin
// Validation, translation, etc, as needed
Result := FData;
end;
procedure TThisClass.SetData(const AData: type) ;
begin
// Validation, translation, etc, as needed
FData := AData;
end;
When a program uses a propertys value, this value is transferred as the return value of the Get function (method). The Get can perform whatever intermediate actions are needed and assigns a value derived from the FData variable to the functions Result variable in order to return the value. The data types of the property and the underlying data field are usually the same but can differ if necessary.
Direct property mapping is specified as follows:
...When a program assigns a value to a directly-mapped property, this value is transferred directly to the private FData variable. When a program uses a propertys value, the value of FData variable is transferred directly without intermediate processing. No validation or manipulation is possible and the data types of the property and the underlying data field are the same and cannot differ.
private
FData: type;
public
property Data: type read FData write FData;
...
If only a read mapping is specified (eg with constructed values), the property is readonly, and vice versa, although the object itself can still both read and write to the underlying data field.
Delphi provides a facility called Class Completion that can save a lot of typing in specifying properties (and in generating method skeletons): refer to online Help for details.
Synopsis of properties
This synopsis summarises what we have seen already in this chapter and mentions a number of points that will become clearer as we move through the module.- Properties can be accessed by mapping methods or by direct access.
- A property can represent a (private) underlying data attribute or a constructed or modified value.
- Properties can be Read/Write, Read only, or Write only.
- Where appropriate, direct access saves coding. If necessary, direct access properties can later be extended to mapped access (by writing access methods) without affecting those parts of the program that already use the properties.
- Mapping methods can be used to construct or modify data, to perform type conversion, and to provide access control and validation whenever the property is accessed.
- Constructed values require a read access method to construct the value and typically do not have write access. (Well see this later.)
- A superclasss properties become part of the subclass definition in the same way that conventional data fields and methods do.

