Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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++?
Posted
Updated 16-Sep-10 12:53pm
v2

Below a link that will give you a good explanation about this. Search for "Event Creation" and here is explained that you must declare a pointer to the event and shown how this is done in the initialize.

http://www.functionx.com/vccli/general/delevents.htm[^]

Good luck!
 
Share this answer
 
v3
Comments
Festering 16-Sep-10 15:16pm    
Sorry, this doesn't make any sense to me, maybe I wasn't clear. I'm writing managed c++ with VS 2008, with the :clr/safe compiler option. The page you've indicated is something else, is it old maybe? It's using "new" and "*" so I don't think it's relevant to my situation.
E.F. Nijboer 17-Sep-10 5:08am    
But I think the information is still valid. An event can have multiple delegates that are added using the += operator and cannot be null.
Festering 17-Sep-10 6:22am    
But the question is, how do you tell in managed C++ whether an event has any delegates?
E.F. Nijboer 17-Sep-10 10:43am    
As you already found yourself the delegate collection doesn't provide that much information like count, length or whatever. The solution is to wrap this into your own event class that can provide you more information. Well, hopefully you got the info needed. Good luck!
Sergey Alexandrovich Kryukov 11-Feb-11 11:27am    
E.F., "page not found"
Ok I sorted this out so I'm going to go ahead and answer it myself.

After some more searching on Compiler Error C3918 I found this

http://bytes.com/topic/net/answers/548954-how-test-if-delegate-event-empty[^]

which explains how you need to create a custom event. Problem solved.


I also found this

http://msdn.microsoft.com/en-us/library/w901cc8a.aspx[^]

which presents the source of the compiler error but gives no solution (thanks)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900