c# events - 2 independent classes listening to each other?

  • Thread starter Thread starter gavin
  • Start date Start date
G

gavin

hi guys, I have a query about c# event handling that should be obvious
I am sure - but I am confused :-)

Lets say I have two classes.

One is a user control with a tabcontrol on it
The other is a user control with a treeview on it.

Now, both of these controls are placed on a form. Both classes raise
events e.g. when the tab is changed on the first control, the tree
control wants to know about it and vice versca.

so far so good...

Now, given that these controls know NOTHING about each other, how on
earth do I register each as a listener of the other? Neither has a
reference to the other one.

Since they are both placed on the containing form I am assuming that
this containing form must act as an event proxy - i.e. it will listen
for all events raised and call the relevant public handler in each user
control embedded on it (since it has references to all of them).

Am I talking rubbish or is there a better way? In all the examples I
see its one way only, the subscriber MUST know about the event raising
class but my situation is that they both must subscribe to each other.

thanks for reading - having a bad code day :-)
 
Gavin,

You could have the control listen to events on the other control, but
the limitation here is that there are no methods exposed on these controls
which will fit the event handler signature (you can't just assign methods to
call arbitrarily).

So, like you said, you will have to listen for relevant events, and then
make the call on the appropriate controls when those events are fired, just
like you would anything else.

Hope this helps.
 
What you are referring to is the Observer pattern. Microsoft has a
article that explains the pattern and how to do this with events and
delegates.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/observerpattern.asp

You are essentially correct, in that the they must be wired up in the
aspx page. This article takes it a step further and puts a method in
the "Observer object" that acts as the delegate that "listens" for the
event in your second object.
 
hi! thanks for the help guys - I should have mentioned that its a c#
windows application rather than as ASPX, I assume the observer pattern
as you describe above still holds?

thanks!
 
john_teague said:
What you are referring to is the Observer pattern. Microsoft has a
article that explains the pattern and how to do this with events and
delegates.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/observerpattern.asp

You are essentially correct, in that the they must be wired up in the
aspx page. This article takes it a step further and puts a method in
the "Observer object" that acts as the delegate that "listens" for the
event in your second object.

I'm sorry, but I don't think this is the right answer. Of course, an
object listening to events from another object is an example of the
observer pattern, but the specific problem here is that the objects don't
have any reference to each other.

I believe a much better pattern to suggest as a solution to the specific
problem is the Mediator pattern. There's a good description with a quick
introduction at the following URL:
http://my.execpc.com/~gopalan/design/behavioral/mediator/mediator.html



Oliver Sturm
 
Oliver said:
I'm sorry, but I don't think this is the right answer. Of course, an
object listening to events from another object is an example of the
observer pattern, but the specific problem here is that the objects don't
have any reference to each other.

I believe a much better pattern to suggest as a solution to the specific
problem is the Mediator pattern. There's a good description with a quick
introduction at the following URL:
http://my.execpc.com/~gopalan/design/behavioral/mediator/mediator.html



Oliver Sturm


Hi again, I just read about the mediator pattern and it does seem to
work well for my needs, I rattled yup a quick prototype and am quite
happy with how it hangs together.

I do appreciates everyones help on this, thanks!
 
I think you're right. I haven't seen the mediator before, but looks
much better in this case. Sorry, I'm a web developer, so I hear
controls and assume web controls.

I'd like to see your proto type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top