Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass objects in EventArgs

Tags:

c#

eventargs

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?

like image 807
GPGVM Avatar asked Feb 20 '13 10:02

GPGVM


People also ask

What is sender As object E as EventArgs?

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.

What does EventArgs mean?

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.

What is EventArgs in VB?

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.


2 Answers

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.

like image 99
Daniel Hilgarth Avatar answered Oct 01 '22 04:10

Daniel Hilgarth


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; 
like image 42
Joe.P Avatar answered Oct 01 '22 02:10

Joe.P