#include "stdafx.h"
using namespace System;
ref class MyEventArgs : EventArgs
{
public:
property String^ Info;
MyEventArgs(String^ info)
{
Info = info;
}
};
delegate void MyEventHandler(Object^ sender, MyEventArgs^ args);
ref class EventSender
{
public:
event MyEventHandler^ MyEvent;
void Fire(MyEventArgs^ args)
{
MyEvent(this, args);
}
};
ref class EventReceiver
{
public:
void OnMyEvent(Object^ sender, MyEventArgs^ args)
{
Console::WriteLine("My Event with info: " + args->Info );
}
void SetUpToReceive(EventSender^ sender)
{
sender->MyEvent += gcnew MyEventHandler(this, &EventReceiver::OnMyEvent);
}
};
int main()
{
EventReceiver^ receiver = gcnew EventReceiver();
EventSender^ sender = gcnew EventSender();
receiver->SetUpToReceive(sender);
MyEventArgs^ myargs = gcnew MyEventArgs("abc");
sender->Fire(myargs);
}
|