I have multiple forms, 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 conforming solution. (maybe by events?)
I'm using more than these forms, but for the example, these seemed to be enough.
//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 void 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
}
MainForm
should temporarily show theLoginForm
, and not what you have - aLoginForm
permanently shows theMainForm
. I would simplify things as much as possible first. \$\endgroup\$LoginForm
does not need it. It should spin around and wait until the login succeeds. What about the other form? Give me an example of the type of communication that goes on. \$\endgroup\$