Property Access

Every property has a read specifier, a write specifier, or both. These are called access specifiers and they have the form read fieldOrMethod write fieldOrMethod where fieldOrMethod is the name of a field or method declared in the same class as the property or in an ancestor class.

  • If fieldOrMethod is declared in the same class, it must occur before the property declaration. If it is declared in an ancestor class, it must be visible from the descendant; that is, it cannot be a private field or method of an ancestor class declared in a different unit.
  • If fieldOrMethod is a field, it must be of the same type as the property.
  • If fieldOrMethod is a method, it cannot be dynamic and, if virtual, cannot be overloaded. Moreover, access methods for a published property must use the default register calling convention.
  • In a read specifier, if fieldOrMethod is a method, it must be a parameterless function whose result type is the same as the property's type. (An exception is the access method for an indexed property or an array property.)
  • In a write specifier, if fieldOrMethod is a method, it must be a procedure that takes a single value or const parameter of the same type as the property (or more, if it is an array property or indexed property).

For example, given the declaration property Color: TColor read GetColor write SetColor; the GetColor method must be declared as function GetColor: TColor;

and the SetColor method must be declared as one of these:

procedure SetColor(Value: TColor); procedure SetColor(const Value: TColor);

(The name of SetColor's parameter, of course, doesn't have to be Value.)

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.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment