Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,
I am porting a C# project to C++/CLI project by using a conversion tool.
In C# project has a code section in which ‘Event’ is compared with ‘null’ in an ‘if’ condition.

public event RawFileReadHandler EventRead;
if ( this.EventRead != null )
{
  this.EventRead( this, new RawFileReadArgs( total, current++, file, abort ) );
}


Same code section was converted in C++/CLI as:

public:
    event RawFileReadHandler ^EventRead;
if (this->EventRead != nullptr)
{
  this->EventRead(this, gcnew RawFileReadArgs(total, current++, file, abort));
}


But, this ‘if’ statement in the converted code gives a compile time error as, “Usage requires 'member' to be a data member”.
I guess it is not allowing ‘Event’ to be compared in ‘if’ condition with ‘nullptr’.
Is there any other way by which this ‘Event’ comparison with ‘nullptr’ can be written in C++/CLI?
Posted
Updated 20-Mar-11 23:46pm
v2
Comments
Sergey Alexandrovich Kryukov 21-Mar-11 6:07am    
Pretty good question, my 5.
--SA

Good question. The answers were given to a similar question here: Check for null delegate in C++.NET[^]

—SA
 
Share this answer
 
Comments
Olivier Levrey 21-Mar-11 6:12am    
Goog link. A 5. But if the test was there just to prevent a null exception, then all OP needs is to remove it: it is like in C++/CLI the check for null is done internally.
Sergey Alexandrovich Kryukov 21-Mar-11 6:20am    
Exactly. After I post it, I was looking for the reference to remember what was answered. The answers were not so good. (My fault, I forgot it and did not check up).
Thank you.
--SA
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:
C++
private:
    // This is your private event.
    System::EventHandler^ privateHandler;

public:
    // This is the public event.
    event System::EventHandler^ PublicHandler
    {
        void add(System::EventHandler^ handler )
        {
            //just attach user handler to your private event
            privateHandler += handler;
        }
        void remove( System::EventHandler^ handler )
        {
            //just detach the user handler from your private handler
            privateHandler -= handler;
        }
        void raise(Object^ sender, EventArgs^ e)
        {
            //raise your private event
            privateHandler(s, e);
        }
    }


Checking the invocation list:
if (privateHandler->GetInvocationList()->Length == 0)
{
    //there is no handler attached to this event
}
else
{
    // at least one handler is attached to this event
    // call the handlers or do whatever you want...
    privateEvent(...);
}


I hope this answers your question.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 21-Mar-11 6:21am    
Correct. My 5.
I suggest OP use it and formally accept this Answer.
--SA
Olivier Levrey 21-Mar-11 6:29am    
Well actually everything is written in your link. But the answer OP is looking for is inside the question and not the answers ;)
Thanks for the vote.
Kedar Kumbhar 21-Mar-11 6:29am    
Actually, I need to check if the event is empty or not. Based on this the next statement will execute. This project is a porting project. The C# project contains lots of such 'Event' condition check with 'null'. I am desperately seeking for a solution. Can't change the implementation much...
Olivier Levrey 21-Mar-11 6:32am    
Did you read carefully? You just need to remove the if line. Of course you will keep the call to the event but you don't need the if test (the if is done automatically).
Kedar Kumbhar 21-Mar-11 8:36am    
No, I get it. Even I removed the 'If' condition. But, the next statement (just after 'if')is unexpectedly getting executed. This is what I don't want to happen. If the event is null then I don't want object to be reinitialized (happening in the next statement).
I guess I will have to look into the link suggested by SA & you.
But, still in the link there is no way suggested to solve this issue.
Sorry, but can u explain more?
I fear SA will get vibrant after reading this..:-)

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