I have a usercontrol that raises an event after communicating with a web service. The parent handles this event when raised. What I thought would be the proper approach would be to pass the object returned from the webservice to the parent as eventargs???
If this is the proper way I can't seem to find the instructions on how to do so.
UserControl
public event EventHandler LoginCompleted;
then later after the service returns biz object:
if (this.LoginCompleted != null) { this.LoginCompleted(this, new EventArgs() //this is where I would attach / pass my biz object no?); }
Parent
ctrl_Login.LoginCompleted += ctrl_Login_LoginCompleted; ....snip.... void ctrl_Login_LoginCompleted(object sender, EventArgs e) { //get my object returned by login }
So my question is what would be the "approved" method for getting the user object back to the parent? Create a property class that everything can access and put it there?
EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.
EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs.
EventArgs is the base class of all event arguments and doesn't say much about the event. Several events use a derived class to supply more data, eg. a KeyPress event uses the KeyEventArgs class which contains the actual key pressed in its KeyChar property.
You would have to declare your event using EventHandler<T>
where T
is your class that derives from EventArgs
:
public event EventHandler<LoginCompletedEventArgs> LoginCompleted;
LoginCompletedEventArgs
could look like this:
public class LoginCompletedEventArgs : EventArgs { private readonly YourBusinessObject _businessObject; public LoginCompletedEventArgs(YourBusinessObject businessObject) { _businessObject = businessObject; } public YourBusinessObject BusinessObject { get { return _businessObject; } } }
Usage would be like this:
private void RaiseLoginCompleted(YourBusinessObject businessObject) { var handler = LoginCompleted; if(handler == null) return; handler(this, new LoginCompletedEventArgs(businessObject)); }
Please notice how I implemented RaiseLoginCompleted
. This is a thread-safe version of raising the event. I eliminates a possible NullReferenceException
that can occur in a race condition scenario where one thread wants to raise the event and another thread un-subscribes the last handler after the if
check but before actually invoking the handler.
Well, you could do all that or you could define a delegate as your EventHandler and define your properties in its signature.
Such as:
public delegate void MyEventEventHandler(int prop1, string prop2, object prop3...); public event MyEventEventHandler MyEvent;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With