Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EventArgs to pass information back to invoking class

Tags:

c#

eventargs

Is it frowned upon to modify EventArgs in event handlers for the purpose of passing information back to the class invoking the event?

For instance, if I have a low-level communication class needing to validate a certificate for SSL but it has no way of knowing what a valid certificate looks like since that is the knowledge of the different users of the class.

class ValidationEventArgs : System.EventArgs {     public X509Certificate Certificate { get; set; }     public bool Valid { get; set; } } 

Then in the using objects they hook up to the event, and check it somehow changing the Valid flag to indicate if the certificate is acceptable or not.

comms.ValidationEvent += CertValidationHandler;      void CertValidationHandler(ValidationEventArgs args) {     if (args.Certificate.Issuer.Contains(COMPANY_NAME))         args.Valid = true; } 

I have found references of EventArgs being used like this but I have also seen people saying it is not recommended.

Edit: Maybe I should clarify that this is not about inheriting EventArgs, but using them as a bi-directional channel of communication. As others commented this is acceptable and whatever noise Google picks up to the opposite is probably just people having misunderstood/misused the concept and now has the same personal crusade against as goto.

like image 454
r_ahlskog Avatar asked Feb 16 '12 08:02

r_ahlskog


People also ask

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 does EventArgs E mean in C#?

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 base class do you use to convey information for an event?

EventArgs is a base class for conveying information for an event.


2 Answers

Ask yourself the following question, "when I publish an event, do I want any subscriber to alter any of the EventArgs values"? If the answer is no, e.g. you are broadcasting readonly information then make the class immutable, however if you need some feedback from a subscriber then make the properties which need to be altered mutable.

To clarify, in a broadcast example, we want to tell any subscriber something but not let them alter the value.

public class ProgressEventArgs : EventArgs {     public ProgressEventArgs(int current)     {         this.Current = current;     }      public int Current { get; private set; } } 

Equally though, we could also raise an event to ask for information the class itself does not know.

public class FeedbackEventArgs : EventArgs {     public bool ShouldContinue { get; set; }     public string Reason { get; set; } } 
like image 64
Trevor Pilley Avatar answered Sep 19 '22 12:09

Trevor Pilley


You can use the EventArgs class through the Generic Types approach. In this sample, i will use the Rect class with as return type:

public EventHandler<Rect> SizeRectChanged; 

Raising the event:

if(SizeRectChanged != null){    Rect r = new Rect(0,0,0,0);    SizeRectChanged(this,r); } 

Listening the event:

anyElement.SizeRectChanged += OnSizeRectChanged;  public void OnSizeRectChanged(object sender, Rect e){     //TODO abything using the Rect class     e.Left = e.Top = e.Width = e.Height = 50; } 
like image 37
jupi Avatar answered Sep 20 '22 12:09

jupi