|
To | Do this |
---|---|
Define a delegate. | Use the __delegate keyword with a function prototype. For example: __delegate void DelegateOne(double d); |
Create a delegate bound to a static class member. | Use new to create a Delegate object, passing 0 for the first parameter and the address of the static function as the second parameter. For example: DelegateOne* pDel = new DelegateOne(0, &MyClass::MyFunc); |
Create a delegate bound to a non-static class member. | Use new to create a Delegate object, passing a pointer to the instance for the first parameter and the address of the member function as the second parameter. For example: DelegateOne* pDel = new DelegateOne( pMyObject, &MyClass::MyOtherFunc); |
Execute the function bound to a delegate. | Use the delegate’s Invoke function, passing any parameters required. For example: pDel->Invoke(22.7); |
Create an event. | First define a delegate to define the handler routine for this event, as follows: __delegate void ClickHandler(int, int); Then, in the event source class, use the __event keyword to define an event object, like this: __event ClickHandler* OnClick; |
Raise an event. | Use the event object as if it were a function, passing any parameters. For example: OnClick(xVal, yVal); |
Subscribe to an event. | Use the += operator. For example: mySrc->OnClick += new ClickHandler(this, &myHandler); |
Unsubscribe from an event. | Use the -= operator. For example: mySrc->OnClick -= new ClickHandler(this, &myHandler); |
|