next up previous contents index
Next: Statements controlling program flow. Up: Classes Previous: Class definitions

Properties

Classes can contain properties as part of their fields list. A property acts like a normal field, i.e. you can get or set it's value, but allows to redirect the access of the field through functions and procedures. Moreover, properties can be read-only.

the prototype declaration of a property is

property Name : Type [Read ReadSpecifier [write WriteSpecifier]];
A ReadSpecifier is either the name of a field that contains the property, or the name of a method function that has the same return type as the property type. In the case of a simple type, this function must not accept an argument.

A WriteSpecifier is optional: If there is no write specifier, the property is read-only. A write specifier is either the name of a field, or the name of a method procedure that accepts as a sole argument a variable of the same type as the property. You cannot specify only a write specifier.

The section (private, published in which the specified function or procedure resides is irrelevant. Usually, however, this will be a protected or private method.

Example: Given the following declaration:

Type
  MyClass = Class
    Private
    Field1 : Longint;
    Field2 : Longint;
    Field3 : Longint;
    Procedure  Sety (value : Longint);
    Function Gety : Longint; 
    Function Getz : Longint;
    Public
    Property X : Longint Read Field1 write Field2;
    Property Y : Longint Read GetY Write Sety;
    Property Z : Longint Read GetZ;
    end;

Var MyClass : TMyClass;
The following are valid statements:
Writeln ('X : ',MyClass.X);
Writeln ('Y : ',MyClass.Y);
Writeln ('Z : ',MyClass.Z);
MyClass.X:=0;
MyClass.Y:=0;
But the following would generate an error:
MyClass.Z:=0;
because Z is a read-only property.

What happens in the above statements is that when a value needs to be read, the compiler inserts a call to the various getNNN methods of the object, and the result of this call is used. When an assignment is made, the compiler passes the value that must be assigned as a paramater to the various setNNN methods.

Because of this mechanism, properties cannot be passed as var arguments to a function or procedure, since there is no known address of the property (at least, not always).

You can also have array properties. These are properties that accept an index, just as an array. Only now the index doesn't have to be an ordinal type, but can be any type. An array Property is defined as follows:

property Name[Index : IndexType]: Type [Read ReadSpecifier [write WriteSpecifier]];
A ReadSpecifier is the name method function that has the same return type as the property type. The function must accept as a sole arguent a variable of the same type as the index type. You cannot specify fields as arrar types.

A WriteSpecifier is optional: If there is no write specifier, the property is read-only. A write specifier is the name of a method procedure that accepts two arguments: The first argument has the same type as the index, and the second argument is a variable of the same type as the property type. You cannot specify only a write specifier.

As an example, see the following declaration:

Type TIntList = Class
      Private
      Function GetInt (I : Longint);
      Function GetAsString (A : String) : String;
      Procedure SetInt (I : Longint; Value : longint;);
      Procedure SetAsString (A : String; Value : String);
      Public
      Property Items [i : Longint] : Longint Read GetInt Write SetInt;
      Property StrItems [S : String] : String Read GetAsString Write SetAsstring;
      end;

Var AIntList : TIntList;
Then the following statements would be valid:
AIntList.Items[26]:=1;
AIntList.StrItems['twenty-five']:='zero';

Writeln ('Item 26 : ',AIntList.Items[26]);
Writeln ('Item 25 : ',AIntList.StrItems['twenty-five']);
While the following statements would generate errors:
AIntList.Items['twenty-five']:=1;
AIntList.StrItems[26]:='zero';
Because the index types are wrong.

Array properties can be declared as default properties. This means that it is not necessary to specifiy the property name when assigning or readin it. If, in the previous example, the definition of the items property would have been

 Property Items [i : Longint] : Longint Read GetInt Write SetInt; Default;
Then the assignment
AIntList.Items[26]:=1;
Would be equivalent to the following abbreviation.
AIntList[26]:=1;
You can have only one default property per class, and descendent classes cannot redeclare the default property.


next up previous contents index
Next: Statements controlling program flow. Up: Classes Previous: Class definitions

Michael Van Canneyt
Thu Sep 10 14:02:43 CEST 1998