You use delegates in C# like so (code straight from VS help) -
public delegate void FireEventHandler(object sender, FireEventArgs fe);
public event FireEventHandler FireEvent;
and to raise the event -
if (FireEvent != null)
FireEvent(this,args);
You have to check it's not null to avoid NullReferenceExceptions if the event has no delegates. (There are niceties here about copying FireEvent before testing for nullness but we'll ignore that for now)
The C++ equivalent looks like this (again from VS help)
delegate void FireEventHandler(Object^ sender, FireEventArgs^ fe );
event FireEventHandler^ FireEvent;
and to invoke -
FireEvent( this, fireArgs );
There's no checking for null here. You don't need to (you don't get NullReferenceExceptions on an empty event) but you might want to, e.g. to avoid allocating event args or anything else you need to do before raising the event.
But you can't. You might be tempted to try -
if (FireEvent)...
and you get a compiler error
error C3918: usage requires 'FireEvent' to be a data member
However, if you don't use the event keyword, i.e.
delegate void FireEventHandler(Object^ sender, FireEventArgs^ fe );
FireEventHandler^ FireEvent;
then you can check for nullness and everything appears to work, including subscribing to the event from c#.
But all documentation I've looked as says you have to use the event keyword, and not using it has problems with inheritance and accessing the events from C# code.
So my question is, what's going on here? Ideally, how can I perform the equivalent of
if (FireEvent != null)
in C++?