CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    2

    Decoupling GUI from logic question

    I have a simple program that comprises a simple GUI (one form) that has a SerialPort object.

    A button on the GUI activates the SerialPort's Send method

    An event fired by the serialPort object 'listens' for the reply

    // When data is recieved through the port, call this method
    SerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

    The port_DataReceived method amongst other things writes the data received to the GUI

    Now I want to decouple the GUI from the Logic
    If the GUI object creates a Logic object, it can call the Send method no problem.

    question is: what is the best way to get the GUI notified that the Logic has received a message given that the Logic doesnt know anything about the GUI?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Decoupling GUI from logic question

    Your post contains the answer.....

    How does the SerialPort object let your program know that data is there....when it is obvious that the writers of the serial port object had absolutely no knowledge about your program (or even your existance in all likelyhood....)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Decoupling GUI from logic question

    Your logic should be in a separate class. Implement an event on your class which the GUI hooks into.

    e.g.

    Code:
    public class SerialPortLogic
    {
        public delegate void MessageReceivedHandler(object sender, byte [] data);
    
        public event MessageReceivedHandler MessageReceived;
    }
    There's a tutorial on custom events here : http://msdn.microsoft.com/en-us/libr...39(VS.71).aspx.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Oct 2008
    Posts
    2

    Re: Decoupling GUI from logic question

    Thanks for replies.

    what I am not understanding at the mo is - if the Logic object creates the SerialPort object then the port_DataReceived method in Logic can do its stuff (write to a database) but cannot assign to the GUI as it knows nothing of that. I need a 'listener' in the GUI object too somehow?

    Confused, guess I need to do some more reading, will look at that msdn link.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Decoupling GUI from logic question

    Quote Originally Posted by darwen
    Your logic should be in a separate class. Implement an event on your class which the GUI hooks into.
    Ahhhh.you gave it away....

    By the way...Your code is the OLD (.Net 1..x), the following is recommended for 2.0 and later...
    Code:
    public class SerialPortLogic
    {
        public class MessageArgs : EventArgs
        {
             // necessary data.
         }
        public event EventHandler<MessageArgs> MessageReceived;
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Decoupling GUI from logic question

    An event is the answer. In the Logic class, provide an event which will be fired if data is recieved and written to db. GUI can register an event handler with this event and Logic still doesn't know anything about the GUI or whoever is registered to the event. It just fire it regarless how many event handlers are attached to it. And of course, you can provide some extra information in the event args, as it was sugested before.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured