Click here to Skip to main content
13,091,300 members (49,746 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In C#, one can define an event delegate & handler thusly:
class SomeClass
    public delegate void SomeEventHandler(object sender, int n);
    public event SomeEventHandler SomeEvent;
    public void RaiseEvent(int n)
        if (SomeEvent != null)
            SomeEvent(this, n);

then some consuming code could do this:
SomeClass sc = new SomeClass();
sc.SomeEvent += new SomeClass.SomeEventHandler(sc_OnSomeEvent);

which would cause sc_OnSomeEvent() to be called with 2.

I'm having some trouble writing this in C++/CLI.
Here's what I have so far:
public ref class CppCliClass
    delegate void CppCliEventHandler(int n);
    event CppCliEventHandler^ OnCppCliEvent;
    void RaiseCppCliEvent(int n)

but I'm having trouble writing RaiseCppCliEvent().
Based on the C# syntax, I'd expect it to be coded like this:
void CppCliClass::RaiseCppCliEvent(int n)
    if(OnCppCliEvent != nullptr)
        OnCppCliEvent(this, n)

but I get these compiler errors:
error C3918: usage requires 'CppCliClass::OnCppCliEvent' to be a data member
error C2660: 'CppCliClass::OnCppCliEvent::raise' : function does not take 2 arguments

Any suggestions on the correct C++/CLI calling syntax for OnCppCliEvent() from within RaiseCppCliEvent() ?
Posted 21-Oct-11 9:35am
Simon Bang Terkildsen 22-Oct-11 16:12pm
For the comment you posted that I guess you subsequently deleted. My answer is perfectly valid, it just doesn't cover the issue mentioned by SA.
So what you were trying not to be, you turn out to be.
Member 11057470 18-Sep-14 6:02am
Can i use C++ objects and call their methods in C++/CLI code.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Yes, this is well-known problem — C++/CLI is very different. Solution is also well-known, but it might look very unusual: not only you are not allowed to do the check like if(OnCppCliEvent != nullptr), you also never need it!

In other words, just delete this check — the code will work correctly. If event handler is not added, the code invoking event will not be called, automatically. Hard to believe, but try it!

Here is the simplest usage pattern:

ref class EventSample {
    event System::EventHandler^ SomeEvent;
    void FireEvent() {
        //no check-up for nullptr SomeEvent is required here
        //event instance is not a method, if it won't be called in the line above it it's null:
        SomeEvent(this, gcnew System::EventArgs());

Event instance is not a method, it is a instance of some (hidden, auto-emitted per each event argument class used) class which has an invocation list in it, event per se is not called, handlers stored in the invocation lists are. I explained it in my article: Dynamic Method Dispatcher[^]. Sorry, the article in C# where the check for null is actually allowed and needed, but it is not so in C++/CLI.

DLChambers 22-Oct-11 15:30pm
Thanks for the answer! To keep moving while awaiting help, I coded it w/o the null check, but it felt strange. On your suggestion, I did try invoking w/o any handlers, and it didn't throw an exception, so I guess it's not needed :)
SAKryukov 22-Oct-11 21:32pm
You are welcome.
That was wise of you to perform this test. All correct.

Good luck, call again,
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

CppCliEventHandler only takes an integer as parameter update it to reflect your new design.
delegate void CppCliEventHandler(CppCliClass^ sender, int n);
SAKryukov 21-Oct-11 22:45pm
Simon, I think you miss the essence of the problem: the check of delegate instance for null is not allowed in C++/CLI. This is very interesting problem, the solution is well-known (but not so popular, because there are not so many good C++/CLI developers).

Please see my solution -- it addresses the problem and is interesting to get familiar with.
Simon Bang Terkildsen 22-Oct-11 9:56am
Ah ok, I've never used C++/CLI other than when I've replied to questions here.
However the delegate the OP defines still has to be defined as I've defined it or he will still get the posted exception.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 21 Oct 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100