Properties are class members that provide access to object characteristics. Properties are a natural extension of fields because they provide similar syntax for working with them. However, unlike fields, properties do not denote storage locations. Instead, properties have access methods that specify the statements to be executed when property values are read or written. Properties thus provide a mechanism for associating actions with the reading and writing of object's attributes.
A property declaration should include the property type, name, access methods and optionally parameters.
When a property declaration includes the Shared modifier, this property is named a static property. When no Shared modifier is present, the property is named an instance property.
A static property does not operate on a specific instance, and it is a compilation error to use the Self structure in static property accessors. An instance property operates a given instance of a class, and that instance can be accessed using the Self structure.
The access to properties is determined by access methods specified in the property code. Each of declarations consists of the keyword Get (property is read-only) or Set (property is write ready). To change property value, in the body of Set use the Value keyword.
Class SharedProperty
Shared s: string;
o: object;
//Static property
Shared Public Property Version: string
Get
Begin
Return s;
End Get
Set
Begin
s := Value;
End Set
End Property;
//Instance property
Public Property Object: object
Get
Begin
Return Self.o;
End Get
Set
Begin
Self.o := Value;
End Set
End Property;
End Class;
Sub Test();
Var
Obj: SharedProperty = New SharedProperty();
Begin
SharedProperty.Version := "Class";
Obj.Object := "Object";
End Sub;
A property, which signature contains parameters, is named an indexed property. Declaration of an indexed property enables it to be used in the same way as an array. The Out and Var modifiers cannot be used when determining property parameters.
Parametric properties overloading is permitted, that is, declaring several properties with the same name but different parameters signatures.
Class IndexProperty
i: array Of integer;
Public Constructor IndexProperty(Length: integer);
Begin
i := New integer[Length];
End Constructor;
//Indexed property
Public Property Item[Index: integer]: integer
Get
Begin
Return i[Index];
End Get
Set
Begin
i[Index] := Value;
End Set
End Property;
//The first property GetNumCalculate with parameters of the Integer type
Public Property GetNumCalculate[x: Integer; y: Integer]: Double
Get
Var
Result: Double;
Begin
//Some calculations of parameters with the Integer type
Return Result;
End Get
End Property;
//The overloaded property GetNumCalculate with parameters of the Double type
Public Property GetNumCalculate[x: Double; y: Double]: Double
Get
Var
Result: Double;
Begin
//Some calculations of parameters with the Double type
Return Result;
End Get
End Property;
End Class;
Sub Test();
Var
Obj: IndexProperty = New IndexProperty(1);
Result: Double;
Begin
Obj.Item[0] := 100;
//Get value of the first property GetNumCalculate
Result := Obj.GetNumCalculate[100, 200];
//Get value of the overloaded property GetNumCalculate
Result := Obj.GetNumCalculate[100.5, 200.5];
End Sub;
See also: