Structures

Structures are similar to classes as they represent data structures and methods that are used to work with them. However, unlike classes, structures are value types and do not require memory allocation. A variable of the structure type directly contains the structure data, whereas a variable of the class type contains a reference to the data, the latter known as an object. The structure can have the same members declared that can be declared in the class taking into account ceratin differences.

Differences between structures and classes

Structures are particularly useful for small data sets that have value type semantics. Complex numbers, points in a coordinate system, or key-value pairs in dictionaries are all good examples of structures. Key to these data structures is that they have few data members, that they do not require use of inheritance or referential identity.

Assignment copies the value instead of the reference. Similarly to an assignment, when a structure is passed as a value parameter or returned as the result of a function member, a copy of the structure is created. A structure may be passed by reference using reference or output parameters.

A structure declaration consists of an optional number of structure attributes followed by optional Public or Friend access modifiers with the following Struct keyword and structure name. Later, parent interfaces that must be implemented by the structure may be specified via colon. Then follows a structure body followed by the sequence of End Struct words.

Example

Struct UserStructure
    Public Title: string;
    Public Param1: integer;
    Public Param2: double;

    Public Constructor Create(STitle: string; Param1Value: integer; Param2Value: double);
    Begin
        Title := STitle;
        Param1 := Param1Value;
        Param2 := Param2Value;
    End Constructor;
End Struct;

Sub Test();
Var
    StructObj, StructObj1: UserStructure;
Begin
    StructObj := New UserStructure("Custom structure"1003.14);
    StructObj1 := StructObj;
    StructObj1.Title := "Copy of structure with changed values of parameters";
    StructObj1.Param1 := 200;
    StructObj1.Param2 := 2.7;
End Sub;

The example of interface implementation is given in the Interfaces subsection.

See also:

Fore.NET Language Guide