Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Forms delegates
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.
     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 29-Dec-12 10:48am
Edited 29-Dec-12 12:00pm
v3

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
Cyberbird85 at 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 at 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
0 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2014
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