How to have two objects talk to each other
Hi guys,
I am building an application that comprises of a number of "parts" the idea is that these parts are modular and do not know of one another. what I want to do is have some sort of mediator that can recieve messages from module A interpret them and then perform some action on module B.
Has anyone done this type of work before?
Re: How to have two objects talk to each other
yes... what happens is that there's a central part of the app, usually the host... and what it does is it listens to the objects (usually through events)... when a message is recieved, the host then turns around, determines if the message is for itself, another object, or a general broadcast. If the message is for itself, it does what it needs to do... if it's for a specific object, if finds that object and passes the message along. If is a general broadcast, it loops through ALL objects, and sends the message. Each individual object can then determine what action to take from there.
I'll see if I can whip up a sample of this when I get home this evening. It's not hard, but it takes some setup up and pre-planning.
-tg
Re: How to have two objects talk to each other
Cool, ive just been watching a video from mix by the guy who wrote caliburn and my head just exploded :)
1 Attachment(s)
Re: How to have two objects talk to each other
Ok, here it is. It isn't the cleanest code... and it's written in VB 2008. (didn't realize this was in the C# forum until after the fact - I also happen to think faster in VB - if you need help converting it to C#, let me know, will take me a little while, but the concepts should still be sound)
It's pretty straight forward, there's a little creativity going on because I wanted all of the interaction to be through the objects, but I needed the forms for the entry and the display.
Run the project, and click the Start! button... that will create 5 objects, each of which has its own form that gets displayed. From those forms, to send a message enter the NUMBER (not the guid) of the form you want to have recieve the message.... so a number from 1 to 5... and then enter the message... it should then appear on the form corresponding with that object's number.
To send a message to the HOST, enter - as the Receiver number. To send the message to ALL of the objects, enter -1.
The main form will show a message queue of all the messages coming and going.
I hope this helps. Any questions, ask.
Re: How to have two objects talk to each other
Sorry for not getting back to you, this question forms part of a large project so I have been working on the other parts, I'l let you know how I get on with this. VB is fine also :)
Re: How to have two objects talk to each other
Thanks techgnome, That helped alot.