Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.



C#
//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.
     this.Hide();
     mf.Show();
    }      
   }//endofLoginForm
  
 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)
  {
   ISD(string);
  }
 }//endofMainForm
  
 public class NetworkCommunicator
  {
   public void SubscribeToSendMessage(object sender)
    {
     if (sender is MainForm)
     {
        
         (sender as MainForm).ISD += new ImprovedSenderDelegate(SendMessage);
     }
    }
  
   private void SendMessage(string message) 
    {/*magic*/}
  }/endofNetworkCommunicator
  
}    
Posted
Updated 29-Dec-12 11:00am
v3

1 solution

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900