Return a value from an Event -- is there a Good Practice for this?

Hugo picture Hugo · Jul 31, 2009 · Viewed 48.3k times · Source

I'm doing a small multi-threaded app that uses asynchronous TCP sockets, but I will get to the point: I'm using a custom event to read a value from a form and the delegate used by the event returns a string when finished.

My question here is: is that correct? is it OK to return values from the events? or is there a better way to do this? (like using a simple delegate to the form to read the values)

Answer

Dustin Campbell picture Dustin Campbell · Jul 31, 2009

It's often awkward to return values from events. In practice, I've found it much easier to include a writable property on a set of custom EventArgs that is passed to the event, and then checked after the event fires -- similar to Cancel property of the WinForms FormClosing event.