Name

Read Directive

Syntax

property Name: Type ... read Getter ...;

Description

A property’s read directive tells Delphi how to get the property’s value. The Getter can be the name of a field or a method in the class or in an ancestor class.

If the Getter is a field, the field’s type must be the same as the property’s type. The usual access rules apply, so the field cannot be a private field of an ancestor class unless the ancestor class is in the same unit. Typically, the field is a private field of the same class that declares the property. The field can be an aggregate (record or array), and the Getter must specify a record member or array element (at a constant index) of the appropriate type. Records and arrays can be nested.

If the Getter is a method, the method must be a function whose return type is the same as the property type. The method can be static or virtual, but it cannot be a dynamic method.

If the property is indexed or an array property, the Getter must be a method. The first parameter is the index value, which is an Integer. Subsequent arguments are the array indices. The type of each Getter argument must match the type of the corresponding array index.

When the user reads the property value, Delphi gets the value from the Getter field or by calling the Getter method.

Tips and Tricks

  • If you use a Getter field, Delphi compiles all property references into direct field references, so there is no performance penalty for using a property instead of a field.

  • A good programming style is to make all fields private and declare protected, public, or published properties to access the fields. If you need to modify the class at a later date, you can change the field to a method without affecting any code that depends on the class and its property.

Example

See the property keyword for examples.

See Also

Class Keyword, Index Directive, Property Keyword, Write Directive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset