Click here to Skip to main content
13,200,202 members (54,987 online)
Rate this:
Please Sign up or sign in to vote.
Hello everyone!

I'm trying to develop an application in c# i currently have a code similar to the one attached below.
Basically what it does is:
Having multiple form, each of which would like to send message through the network, using my NetworkCommunicator class's SendMessage method.
The solution i described here works, however it is ugly, and bludgeons some of the main concepts of OOP paradigm.
What I'm looking for (and i'm open to any suggestions) is basically to achieve the same goal but in a more elegant, and OOP conform solution. (maybe by events?)

Thank you for your answers in advance, CB.

Ps.: I'm using more than these forms, but for the example these seemed to be enough. Also excuse my english, I'm not a native speaker.

//using statements
namespace examplespace
  public delegate void ImprovedSenderDelegate(string message);  
  public class LoginForm :Form
    private  NetworkCommunicator ncom = new NetworkCommunicator();
    public Ncom
     get { return ncom; }
    private void Login()
     MainForm mf = new MainForm();
     mf.Owner = this;
     //other things i do etc.
 public class MainForm :Form
  public ImprovedSenderDelegate ISD;
  private Load ()
   (this.Owner as LoginForm).NCom.SubscribeToSendMessage(this);
  private void I_Want_To_send_A_Message(string message)
 public class NetworkCommunicator
   public void SubscribeToSendMessage(object sender)
     if (sender is MainForm)
         (sender as MainForm).ISD += new ImprovedSenderDelegate(SendMessage);
   private void SendMessage(string message) 
Posted 29-Dec-12 9:48am
Updated 29-Dec-12 11:00am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can't do anything OO over the network, because you have two different exes running. You can't use delegates between two exes on two machines.
Cyberbird85 29-Dec-12 16:54pm
You might have misunderstood my question. I'm sending simple messages over the network for example "COMMAND|COMMAND_NAME_2|COMMAND_PARAMETER"
i however would like to get the response sent back by the server to other forms on the client after catching it with my network communicator class(on the client) and i would like to use that class to send message to server, from multiple forms(still from the client). Hope i refined my point.
Christian Graus 30-Dec-12 15:18pm
Oh - once you GET the message, you can broadcast it within your app anyway you like, including using delegates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100