Properties Description

Syntax

$ PropertyDeclarations = PropertyDeclaration_Old | PropertyDeclaration_New

 

$ PropertyDeclaration_Old = PROPERTY ident [ Indexes ] ":" ClassType Specifiers [ DEFAULT ";"]

$ Indexes = "(" [ISection {";" ISection}] ")"

$ ISection = ident ":" ClassType

$ Specifiers = ( GET ident | SET ident | GET ident SET ident ) ";"

 

$ PropertyDeclaration_New = PROPERTY ident [ Indexes ] ":" ClassType Specifiers_1 END PROPERTY ident ";" [ DEFAULT ";"]

 

$ Specifiers_1 = ( GetDeclaration | SetDeclaration | GetDeclaration SetDeclaration) ";"

$ GetDeclaration = GET BEGIN StatementSequence END GET

$ SetDeclaration = SET BEGIN StatementSequence END SET

Description

An access to object properties is defined by the property qualifiers - the property may be available only for reading (GET), or only for writing (SET), or available for both reading and writing (GET and SET). The Fore language supports two syntaxes for properties description (See Syntax: $ PropertyDeclarations). 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, the 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.

Example

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 (a read-only property). All values will be stored in an internal symbol array PropValue. The MyArray class uses the old method of property declaration, the MyArray1 class uses the new one. Working with these collections in code is identical.

////Old method for property declaration
Class MyArray: 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 MyArray;

//New method for property declaration
Class MyArray1: 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 MyArray1;

Sub Main;
Var
    Arr: MyArray;
    Arr1: MyArray1;
    s: String;
    i: Integer;
Begin
    Arr := New MyArray.Create(2);
    Arr1 := New MyArray1.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 items
    For Each s In Arr1.Items Do
        Debug.WriteLine(s);
    End For;
End Sub Main;

After starting the Main procedure, two copies of custom collections are created: one copy of MyArray collection and one copy of MyArray1 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:

Descriptions and Syntax Rules | Classes Description