Delegate is an object that refers to a method or a method group. A delegate instance contains an invocation list, which includes one or more methods, each of which is referred to as a callable entity. For instance methods, a callable entity consists of a reference to an object instance and its method. For static methods, a callable entity consists of just a method. Invoking a delegate with a set of parameters causes each of the methods to be invoked, references to which are added to the delegate instance.
A delegate declaration consists of the optional Public or Friend access modifier followed by the Delegate keyword and a delegate name. Then parameters can be optionally specified in parentheses. A delegate and a method are compatible if both the following conditions are true:
They have the same number of parameters of the same types determined in the same order with the same parameter modifiers.
They have the same return type.
Delegate types are implicitly final, so it is not allowed to declare types derived from delegates. It is also not allowed to declare non-delegate types derived from System.Delegate.
A delegate instance is created by specifying the New keyword followed by a delegate name and a method, to which the delegate refers. The methods list can be extended in the form delegate += sub and reduced in the form delegate -= sub.
The created delegate instance always refers to the same object and method(s). When two delegate instances are combined or a delegate instance is deleted from another instance, a new instance is always created with its own invocation list; the invocation lists of operands remain unchanged. When two non-empty delegates are combined, their invocation lists are combined from left to right.
Delegates act as types on description of events.
Delegates are invoked by specifying name of the created delegate instance and a set of parameters values. When a non-empty delegate instance, which invocation list contains one element, is invoked, it invokes the corresponding method with parameters values and returns the same value returned by this method. If an exception is thrown during the invocation, and that exception is not handled within the method that was invoked, the search for an exception handler continues in the body of the method that called the delegate.
Invocation of a delegate instance, which invocation list contains multiple elements, proceeds by invoking each of the methods in the invocation list, synchronously, in order. Each method is invoked with the same set of arguments as was given to the delegate instance. If such a delegate has output parameters (Out) or a returned value, their final values will come from the invocation of the last method in the list. If an exception is thrown during the invocation, and that exception is not handled within the method that was invoked, the search for an exception handler continues in the body of the method that called the delegate. Any methods further down the invocation list are not invoked.
Public Delegate UserEventHandler(x, y, z: integer);
Public Class DelegateSample
//Class event
Public Event OnUserEvent1: UserEventHandler;
Public Constructor UserObject();
Begin
//Set event handlers
//The Sub1 and Sub2 procedures will be executed one by one on event occurrence
OnUserEvent1 := Sub1;
OnUserEvent1 += Sub2;
End Constructor;
//Event handler procedures
Sub Sub1(x, y, z: integer);
Begin
End Sub;
Sub Sub2(x, y, z: integer);
Begin
End Sub;
Shared Sub Sub3(x, y, z: integer);
Begin
End Sub;
//Procedure for creating and invoking delegate instance
Public Sub RunDelegate(x, y, z: integer);
Var
d1, d2: UserEventHandler;
Begin
//Create a delegate instance
d1 := New UserEventHandler(Sub1);
d1 += Sub2;
d2 := New UserEventHandler(Sub3);
d1 += d2;
//Delegate invokation. The Sub1, Sub2 and Sub3 procedures will be executed on invokation
d1(x, y, z);
End Sub;
End Class;
See also: