Creating a Delegate

A delegate instance is created using the operator of delegate instance creation. A created delegate instance refers to:

Once created, a delegate instance always refers to the same object or method. When two delegate instances are combined or a delegate instance is deleted from another instance, a new instance is created with its own invocation list; the invocation lists of the operands remain unchanged.

Example

Delegate UserEventHandler(x, y, z: integer);

Class UserObject
    //Redetermine abstract event
    Public Event OnUserEvent1: UserEventHandler;
    
    Constructor UserObject();
    Begin
        //Set event handlers
        //The following procedures are executed when the event occurs
        //ForEven1 and ForEven2
        OnUserEvent1 := ForEven1;
        OnUserEvent1 += ForEven2;
    End Constructor;

    //Event handling procedures
    Sub ForEven1(x, y, z: integer);
    Begin
        
    End Sub;

    Sub ForEven2(x, y, z: integer);
    Begin
        
    End Sub;
    
    //Procedure of creating and invoking delegate instance
    Sub RunDelegate(x, y, z: integer);
    Var
        d: UserEventHandler;
    Begin
        //Create a delegate instance
        d := New UserEventHandler(ForEven1);
        //Delegate invocation. The ForEven1 procedure is executed on invocation
        d(x, y, z);
    End Sub;
End Class;

See also:

Delegates