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.
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;
this.Hide();
mf.Show();
}
}
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);
}
}
public class NetworkCommunicator
{
public void SubscribeToSendMessage(object sender)
{
if (sender is MainForm)
{
(sender as MainForm).ISD += new ImprovedSenderDelegate(SendMessage);
}
}
private void SendMessage(string message)
{}
}/endofNetworkCommunicator
}