Question

I've been having some trouble with events:

Lets say I have 3 classes 1 is the 'Master' and 2 'slaves'.

The Master instantiates the slaves.

One of the slaves is a debug-display class that reports on various aspects of the running program. The other slave contains its own object with a variable that the debug-display needs to report on.

The first thing that occurred to me is to just expose a property on the slave and slave's-sub class and have the master pass it to the debug-display but that seems like very poor design?

So I did a bit of reading and learned about delegates in C# (I understand the basics and was able to code some simple examples myself).

All the examples I've found involve the listener also being the instigator of the class throwing the exception. So how should I support passing/handling events (or data) between two children?

Any advice is very much appreciated.

PS: I tried to google this but found a tonne of examples that involve events/delegates but not quite like this...

Was it helpful?

Solution

You can wire two different classes together when using events, i.e.

o1.MyEvent += o2.MyHandler;

Just because the WinForms designers always use this.MyHandler doesn't mean it's the only option.

You might also peruse the Microsoft document How to: Implement Property Change Notification

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top