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
.
CancelEventArgs
; it doesn't get much more basic than that. – Twobit