Delegate
Introduction
As a variable can be declared as a pointer so can a function. A pointer to a function is a type of variable whose name can be used as a variable although it is not a traditional variable the others. This concept has always been very helpful in Microsoft Windows programming because it allows the use of callback functions. Thanks to their effectiveness (and legacy code), the concept of callback functions was carried out in the .NET Framework but they were defined with the name of delegate.
A delegate is a special type of user-defined variable that reference a method of a class. There are similarities and differences between a function pointer and a delegate:
Like a function pointer, a delegate is not defined as like a normal function
Like a function pointer, a delegate has the appearance of a variable that is declared as a pointer
Like a function pointer, a delegate can take argument(s)
While a function pointer can be associated with a global function, that is, a function that is not a member of a class, a delegate can be associated, or can reference only, a member of a class
Delegate Declaration
To declare a delegate, you use the __delegate keyword. The basic formula used to create a delegate is:
Access Level __delegate Function-Signature;
The Function-Signature factor is created as you would declare a normal C++ function. This means that the Function-Signature must have
A return type: The return type can be void, one of the primitive data types (int, long, double, short, etc), a .NET Framework type (Double, Decimal, etc), a .NET Framework class, or a class you created
A name: Like every variable, function, or class, a delegate must have a name
Parentheses: Like every function, a delegate must have a name
Optional arguments: If the method(s) that this delegate will reference has/have argument, this delegate must also have argument(s) of exactly the same kind)
After defining the Function-Signature, you must end the delegate declaration with a semi-colon. Here is an example:
__delegate double Addition();
A declaration of a delegate can also use an access level as public or private. If you plan to use the delegate only locally, you can omit the access level or declare it as private, which is the default. If you plan to use the delegate outside of the project in which it is created, you should declare it as public.
After declaring a delegate, remember that it only provides a skeleton for a method, not an actual method. In order to use it, you must define a method that would carry an assignment the method is supposed to perform. That method must have the same return type and the same (number of) argument(s), if any. Here is an example:
private __delegate double Addition();
__gc class MathOperations
{
public:
double Plus()
{
double a = 248.66, b = 50.28;
return a + b;
}
};
After implementing the method, you can associate it to the name of the delegate. To do that, where you want to use the method, first declare a pointer of the type of the delegate using the new operator and calling its default constructor.
Techniques of Using a Delegate