In C#, the check for
null
is required to prevent a null exception. In C++/CLI, it is not needed. Just remove the
if
statement: if the event is empty, then nothing will happen.
Now if you want to check wether the event is empty or not, then SA's link will help.
-----
The link given by SA suggests to use a private event so you can use
GetInvocationList
method and its
Length
property (if it is 0 then no handler is attached to the event).
Refer to this question:
http://www.codeproject.com/Messages/1886045/Why-does-GetInvocationList-work-in-Csharp-but-not-.aspx[
^]
Nish answered that using the
GetInvocationList
works only if you implement the event by yourself.
So I suppose something like that will work:
Declaring the events:
private:
System::EventHandler^ privateHandler;
public:
event System::EventHandler^ PublicHandler
{
void add(System::EventHandler^ handler )
{
privateHandler += handler;
}
void remove( System::EventHandler^ handler )
{
privateHandler -= handler;
}
void raise(Object^ sender, EventArgs^ e)
{
privateHandler(s, e);
}
}
Checking the invocation list:
if (privateHandler->GetInvocationList()->Length == 0)
{
}
else
{
privateEvent(...);
}
I hope this answers your question.