Creating and Using Delegates

I l @ ve RuBoard

Delegates are the .NET equivalent of callbacks and function pointers. They are used primarily to route events to handlers as they arrive .

A delegate declaration describes a function signature with a specific pattern of parameters and a specific return type. Once this pattern is established, it might be used to invoke any function with the same signature. A difference between delegates and callbacks implemented by C++ classes is that, although callbacks in C++ must always be static members of the class, delegates can be used to invoke instance members , too.

A delegate is declared like this:

 __delegate <return type> MyMCDelegate(<parameter list>); 

NOTE

Delegates can have many parameters. The delegate matching pattern relies on the return type and the parameter list. Therefore

 __delegate char MyDelegate(int x, int y, double d) 

can be used on both of the following method declarations:

 char AFunction(int a, int b, double dData) static char Foo(int moo, int hoo, double doo) 

Listing 1.4.8 shows the delegate in action. It illustrates a simple delegate, a delegate that takes input parameters, delegates that return values, and those that return data throughout parameters.

Listing 1.4.8 delegate.cpp : Using Delegates
 #using <mscorlib.dll> using namespace System; #include <stdio.h> // first declare the delegate signatures we will use. __delegate void SimpleDelegate(void); __delegate void DelegateWithParameter(int); __delegate int DelegateWithReturn(int); __delegate void DelegateWithOutParam(int,int *); __gc class AClass { public:     static void StaticHandler(int n)     {         printf("I am a static handler and Simple parameter is %d\ n",n);     }     void InstanceHandler(int n)     {         printf("I am an instance handler and Simple parameter is %d\ n",n);     }     void HandlerA()     {         printf("I am handler (a)\ n");     }     void HandlerB()     {         printf("I am handler (b)\ n");     }     void HandlerC()     {         printf("I am handler (c)\ n");     }     void HandlerD()     {         printf("I am handler (d)\ n");     }     int WithReturn(int x)     {         printf("Returning %d*5\ n",x);         return x*5;     }     void WithOutParam(int x, int *retval)     {         printf("Returning %d*14 through the out parameter\ n",x);         *retval = x*14;     } }; void main() {     // instantiate the handler class..     AClass* pC=new AClass();     // Create the delgates     DelegateWithParameter* sh=            new DelegateWithParameter(NULL,&AClass::StaticHandler);     DelegateWithParameter* ih=            new DelegateWithParameter(pC,&AClass::InstanceHandler);     DelegateWithReturn* wr=new DelegateWithReturn(pC,&AClass::WithReturn);     DelegateWithOutParam* wo=new DelegateWithOutParam(pC,&AClass::WithOutParam);     //Invoke the static...     sh->Invoke(10);     //and the instance handlers.     ih->Invoke(100);     //Now a multicast which is the invocation     //of all the handlers on a list     SimpleDelegate* mc=new SimpleDelegate(pC,AClass::HandlerA);     mc=static_cast<SimpleDelegate*>(Delegate::Combine(mc,                 new SimpleDelegate(pC,&AClass::HandlerB)));     mc=static_cast<SimpleDelegate*>(Delegate::Combine(mc,                 new SimpleDelegate(pC,&AClass::HandlerC)));     mc=static_cast<SimpleDelegate*>(Delegate::Combine(mc,                 new SimpleDelegate(pC,&AClass::HandlerD)));     mc->Invoke(); // One invoke makes four calls     //Now a delegate with an out parameter     printf("Invoking delegate with value 5\ n");     int r = wr->Invoke(5);     printf("WithReturn sent back %d\ n",r);     //finally a delegate with an out parameter     printf("Invoking delegate with value 11\ n");     wo->Invoke(11,&r);     printf("WithOutParam sent back %d\ n",r); } 
I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net