Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the C++/CLI syntax to subscribe for events?

I'm updating some old Managed C++ code with lines like this:

instanceOfEventSource->add_OnMyEvent( 
    new EventSource::MyEventHandlerDelegate(this, MyEventHandlerMethod) );

where

  • EventSource is the class that publishes events
  • instanceOfEventSource is an instance of that class
  • EventSource::MyEventHandlerDelegate is the delegate type for the event
  • MyEventHandlerMethod is a (non-static) method within the current class (of which "this" is an instance) with the signature matching EventSource::MyEventHandlerDelegate

What is the right syntax for this in C++/CLI?

like image 488
Eric Avatar asked Jan 28 '09 22:01

Eric


1 Answers

The syntax is similar to C#'s, in other words, += is overloaded to make this possible:

instanceOfEventSource.MyEvent +=
    gcnew EventSource::MyEventHandlerDelegate(this, &MyClass::MyEventHandlerMethod);

Analogously for removal. Unlike C#, however, you may not omit the explicit instantiation of the event handler delegate so this produces quite long-winded code.

like image 167
Konrad Rudolph Avatar answered Oct 04 '22 14:10

Konrad Rudolph