Problems with, or best practices for, passing data back through eventargs?

350 Views Asked by At

I've got a non-GUI class that generates events as to what it is doing (which are in turn used by a Form to display to the user the progress).

One of the events is a AboutToDoSomethingDestructiveEvent. Now we want to have the Form display a dialog to the user when AboutToDoSomethingDestructiveEvent is raised, asking them if they would like SomethingDestructive to happen. If they select no, then we would set a value on the customer EventArgs and the original form would read that value and then skip doing SomethingDestructive.

Is this a proper use of Events and EventArgs? Are there problems with this approach? Are there any best practices for doing this sort of thing?

3

There are 3 best solutions below

1
On BEST ANSWER

The approach is so good there's even a class in the .NET Framework for this: CancelEventArgs

0
On

The way you are thinking is the proper way to do it. The Console.CancelKeyPress event is essentially the same thing.

Console.CancelKeyPress

0
On

This is a proper approach, as long as you have your own EventArgs, which are inheriting from System.EventArgs. It is very common, the best example I can think of is in PostSharp with the FlowBehavior.