views:

43

answers:

1

This question is only related to the first parameter in any event handler, that is 'object'.

I am just curious to understand the best practice that should be adopted while raising an event.

The case is simple when the event is originated from my object itself, in that case i can simply call... RaiseSomeEvent(this, someArgs).

What should be done in case my object is acting as a middle man, and the event originates from some other object, and the middle man is just responsible for raising it again? In that case, i will have two choice:

1) RaiseSomeEvent(sender, someArgs) // just passing the object that was passed by the source object. 2) RaiseSomeEvent( this, someArgs)

Is there any rule regarding the 'object' param of an event? like, it should have reference to the source object (the one which triggered this event), or i can do it anyway as per my requirement?

+3  A: 

I would use the framework here for inspiration.

Typically, the case is the simplest - the object is the object raising the event.

However, there are cases where objects "filter" events and act as a middle man in the framework. The case that comes to mind is Routed Events in WPF -

In this case, the Framework uses the approach of adding a property in the EventArgs that specifies the original object, and uses "this" for the event. For details, see RoutedEventArgs.OriginalSource.

Reed Copsey