This is the call:
bool isValid = true;
if (this.ExternalConstraint != null)
{
isValid &= this.ExternalConstraint(this, EventArgs.Empty);
}
if (isValid)
{
//...
}
The event look like:
public delegate bool externalConstraint(object sender, EventArgs args);
event externalConstraint ExternalConstraint;
When debugging I notice that all method attached to the event is called but only the last return seem to return to this line : isValid &= this.ExternalConstraint(this, EventArgs.Empty);. How can I get the event to return every method or to handle the all return value to take a decision?
It’s considered bad practice to depend on return values when using events. Rather, you should use a user-defined
EventArgsto get back results.It’s also important to implement some mechanism in which multiple event listeners won’t override each other’s values. Most of the time (with a boolean flag), we will allow (or encourage) users to only explicitly turn it on (never off).
For example, with
CancelEventArgsit’s never a good idea to set it tofalseexplicitly; Users should always set it totrueor do nothing. The same idea should apply here withIsValid.