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. 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.
NOTE. Using Inherited to call the Set part of a basic property is not supported.
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. It means that the Obj(Index) call may be used instead of the Obj.Prop(Index) call.
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.
See also: