A property is a separate type of class members that provide access and change internal data fields.
Access to object properties is determined by the property specifiers Get (read-only) or Set (write). The Fore language supports two syntaxes to describe properties (see example below). When using a new declaration method, the Value keyword must be used in the Set procedure to change a property value. If a property has parameters, their values can also be used to get or change property value. Default parameter values are also taken into account. An access to a property may be readdressed to the object field (if a field name is used as an identifier) or can initialize the call of an object method (if a method name is used as identifier). If a method is called during property writing, the last parameter passed to the method will be the value for writing. When a property is redefined in child classes, Get and Set parts of the basic property can be called using Inherited.
The property may also be indexed. In this case the methods names shall be defined as the property access identifiers. When an indexed property is called, index values are passed to the method as the first parameters.
Use the Default directive only for indexed properties to define a default value. That means that instead of addressing the ClsObj.Prop(Index) procedure, one can address ClsObj(Index). A class can contain only one property by default.
In the example, two classes are declared implementing custom collections of string data. The collection size is specified when a new variable is initialized. The Item property is used to get access to collection elements. This property is available for both reading and writing. This is the default property. (You can access this property without specifying the property itself. You need to specify only the index of the required element). To access the whole collection, use the Items property (read-only property). All values will be stored in an internal symbol array PropValue. The OldArray class uses the old method of property declaration, the NewArray class uses the new one. Working with these collections in code is identical.
Class NewArray: Object
Private PropValue: Array Of String;
Public Constructor Create(Length: Integer);
Begin
PropValue := New String[Length];
End Constructor Create;
Public Property Item(i: Integer): String
Get
Begin
Return PropValue[i];
End Get
Set
Begin
PropValue[i] := Value;
End Set
End Property Item; Default; //Default property
Public Property Items: Array Of String
Get
Begin
Return PropValue;
End Get
End Property Items;
End Class NewArray;
Class OldArray: Object
Private PropValue: Array Of String;
Public Constructor Create(Length: Integer);
Begin
PropValue := New String[Length];
End Constructor Create;
Sub Set_Item(i: Integer; s: String);
Begin
PropValue[i] := s;
End Sub Set_Item;
Function Get_Item(i: Integer): String;
Begin
Return PropValue[i];
End Function Get_Item;
//Default property
Public Property Item(i: Integer): String Get Get_Item Set Set_Item; Default;
Function Get_Items: Array Of String;
Begin
Return PropValue;
End Function Get_Items;
Public Property Items: Array Of String Get Get_Items;
End Class OldArray;
The example of using classes with new and old method of property declaration:
Sub Main;
Var
Arr: NewArray;
Arr1: OldArray;
s: String;
i: Integer;
Begin
Arr := New NewArray.Create(2);
Arr1 := New OldArray.Create(2);
Arr(0) := "First element";
Arr.Item(1) := "Second element";
Arr1(0) := "First element of the second array";
Arr1.Item(1) := "Second element of the second array";
//First array elements
For i := 0 To Arr.Items.Length - 1 Do
Debug.WriteLine(Arr.Item(i));
End For;
//Second array elements
For Each s In Arr1.Items Do
Debug.WriteLine(s);
End For;
End Sub Main;
After starting the Main procedure, two instances of custom collections are created: one copy of th NewArray collection and one copy of the OldArray collection. The size of each collection is equal to two elements. As the Item property is a default property, you cannot select its values without specifying the name of the property. Values of the elements can be specified using different methods. The set values of the collection elements will be displayed in the development environment console for checking.
The example of redefining a property in a child class with calling Get/Set parts of the basic property:
Class BaseClass: Object
Private PropValue: Double;
// Basic property
Public Property Size: Double
Get
Begin
Return PropValue;
End Get
Set
Begin
PropValue := Value;
End Set
End Property Size;
End Class BaseClass;
Class MainClass: BaseClass
//Redefined property that calls Get and Set of basic property
Public Property Size: Double
Get
Begin
Return Inherited Size
End Get
Set
Begin
Inherited Size := Value;
End Set
End Property Size;
End Class MainClass;
The example of using the Value keyword and property parameters:
Public Class PropertyClass: Object
_val: variant = Null;
Public Property DefaultValue(DefValue: Variant = Null): Variant
Get
Begin
// If the current saved property value is Null, return value of the DefValue parameter as a property value
Return _val = Null ? DefValue : _val;
End Get
Set
Begin
// If the set property value is Null, save values of the DefValue parameter as a property value
_val := Value = Null ? DefValue : Value;
End Set
End Property DefaultValue;
End Class PropertyClass;
See also: