Sign in to follow this  
DrGUI

[.net] Event 'sender' when raised from static method

Recommended Posts

DrGUI    402
Hi When you raise an event, you know you're meant to pass 'this' as the 'sender' parameter, but what do you do if you raise the event from a static function? Seems the only thing you can do really is pass null...

Share this post


Link to post
Share on other sites
DrGUI    402
Afraid not (Find in page hehe) but thanks for the link anyway.

DOn't worry people as I'm not using that event anymore [grin]

Share this post


Link to post
Share on other sites
ernow    732
As the 'raiser' of the event you're free to mark anything as the sender.
It might be an option to pass a Type object containing info on the class that raised the event... (Note I said 'might') Use typeof(...) to create such an object.

Otherwise null is acceptable.

Cheers

Share this post


Link to post
Share on other sites
DrGUI    402
Can I call you Dan?...that was talking about the members of single cast delegates. Perhaps I should have made it clearer that I meant:

you have your event...
public System.EventHandler MyLovelyEvent;

then you raise it...
if (MyLovelyEvent != null)
MyLovelyEvent(this, System.EventArgs.Empty);

However, when raising an event from a static function, you can't use 'this'.

Like ernow says, I would now use null. I was asking because I thought it might be bad practise to pass null :)

If you're interested - I changed from a event to an array of WeakReferences so it referenced objects could be GCed [smile]

Thanks for the replies!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this