How to: Access Events in Interfaces
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Access Events in Interfaces

The accessibility of a raise method is never public and is protected by default. A raise method is private if the event is private, and virtual if the event is declared virtual. If an event is declared in a managed interface, only its add and remove methods can be implemented.


// mcppv2_events2.cpp
// compile with: /clr
using namespace System;

delegate void Del(int, float);

// interface with event and a function to invoke event
interface struct I {
   event Del ^ E;
   void fire(int, float);   

// class that implements interface event and function
ref class EventSource: public I {
   virtual event Del^ E;
   virtual void fire(int i, float f) {
      E(i, f);

// class that defines event handler
ref class EventReceiver {
   void Handler(int i , float f) {

int main () {
   I^ es = gcnew EventSource();
   EventReceiver^ er = gcnew EventReceiver();

   // hook handler to event
   es->E += gcnew Del(er, &EventReceiver::Handler);

   // call event
   es -> fire(1, 3.14);

   // unhook handler from event
   es->E -= gcnew Del(er, &EventReceiver::Handler);



See Also

© 2015 Microsoft