Differences between Structures and Classes

Structures differ from classes in several important ways:

Assignment

Assignment to a variable of a structure type creates a copy of the value being assigned. This differs from assignment to a variable of a reference type, which copies the reference but not the value itself.

Similar 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.

When a structure property is used in the left part of an assignment statement, the instance expression associated with the property must be classified as a variable. If the instance expression is classified as a value, a compile error occurs.

Example

Struct UserStructure
    Public Title: string;
    Public Param1: integer;
    Public Param2: double;
    
    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("User structure"1003.14);
    StructObj1 := StructObj;
    StructObj1.Title := "Copy of a structure with modified parameter values";
    StructObj1.Param1 := 200;
    StructObj1.Param2 := 2.7;
End Sub;

See also:

Structures