2014-11-21 56 views
0

我有三个类,下面提供了代码。Windows窗体不能从公共方法返回正确的值

网络 - 添加和删除电话,进程呼叫 电话1和电话2可以在添加到网络时相互呼叫。

但我有问题,当我连接两个电话到网络,并试图打电话给phone2,它一直在给我“接收器忙”。我试图做一些调试,并从phone1中调用时读取phone2的状态,但它返回一个空字符串(当它被添加到网络时,实际上应返回“A”)。

任何帮助将不胜感激。

-----网络类------------------

namespace Demo 
{ 
    public partial class network : Form 
    { 
     phone1 p1 = new phone1(); 
     phone2 p2 = new phone2(); 
     public network() 
     { 
      InitializeComponent(); 
     } 

     public Boolean numberValidator(int number) 
     { 

      Boolean exist = false; 
      if (comboBox2.Items.Equals(number)) 
      { 
       exist = true; 
      } 

      return exist; 
     } 

     public void processCall(int rNumber) 

     { 

      if (!numberValidator(rNumber)) 
      { 
       p1.TextBox1.Clear(); 
       p1.TextBox1.Text = "Not connected"; 

       //MessageBox.Show(p2.returnPhoenStatus()); 
      } 

      else 
      { 

        p1.TextBox1.Clear(); 

        p1.TextBox1.Text = "Call in progress"; 

        p2.receiveCall(1); 

        p1.setStatus("Busy"); 
        /* 
        if (p2.btnCallPressStatus()) 
        { 
         p1.TextBox1.Clear(); 

         p1.TextBox1.Text = "Call initiated"; 
        }*/ 

      } 


      } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedIndex == 0) 
      { 
       p1.Show(); 
       comboBox2.Items.Add(1); 
       p1.setStatus("A"); 
      } 
      if (comboBox1.SelectedIndex == 1) 
      { 
       p2.Show(); 
       comboBox2.Items.Add(2); 
       p2.setStatus("A"); 
      } 
     } 
    } 
} 

----------电话1分类 - -------

namespace Demo 
{ 
    public partial class phone1 : Form 
    { 
     public phone1() 
     { 
      InitializeComponent(); 

     } 




     string status; 

     public void setStatus(string Status) 
     { 
      status = Status; 
     } 

     public string returnStatus() 
     { 
      return status; 
     } 

     public void receiveCall(int callerNumber) 
     { 
      setStatus("Busy"); 

      btnCall.Text = "Answer"; 

      textBox1.Text = "Phone " + callerNumber + " Calling."; 

     } 

     public void makeCall(int number) 
     { 
      phone2 p2 = new phone2(); 
      network net = new network(); 

      MessageBox.Show(p2.returnStatus()); // this line not returing status of phone2 
      if (p2.returnStatus() == "A") 
      { 
       net.processCall(number); 
      } 
      else 
      { 
       textBox1.Text = "Receiver Busy"; 
      } 


     } 

     public TextBox TextBox1 
     { 
      get 
      { 
       return textBox1; 
      } 
     } 

     private void btnCall_Click(object sender, EventArgs e) 
     { 
      string number = textBox1.Text; 
      int numberInt = Convert.ToInt16(number); 


      makeCall(numberInt); 
     } 

     string phoneNo = ""; 
     private void btn2_Click(object sender, EventArgs e) 
     { 
      phoneNo = phoneNo + btn2.Text; 

      textBox1.Text = phoneNo; 
     } 
    } 
} 

------------- PHONE2类--------------

namespace Demo 
{ 
    public partial class phone2 : phone1 
    { 
     public phone2() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

回答

0

Piyush有正确的答案,但我想我会添加这个答案作为一个方便的提示,以避免这种错误。

试着写你button1_Click方法是这样的:

private void button1_Click(object sender, EventArgs e) 
{ 
    var i = comboBox1.SelectedIndex; 
    var p = (new [] { p1, p2 })[i]; // Or `var p = i == 0 ? p1 : p2;` 

    p.Show(); 
    comboBox2.Items.Add(i + 1); 
    p.setStatus("A"); 
} 

这样你避免重复代码和所发生的错误输入。

+0

非常感谢您的回复。我正在寻找那样的东西。 – 2014-11-21 11:20:58

2

我认为你正在设定P1的状态。检查网络类中button1_Click方法是否存在条件。 setStatus应该用于P2。

if (comboBox1.SelectedIndex == 1) 
      { 
       p2.Show(); 
       comboBox2.Items.Add(2); 
       p2.setStatus("A"); 
      } 
+0

非常感谢您的回复。但即使在您建议我无法从phone1拨打phone2之后。正如前面提到的,我试图将MessageBox.Show(p2.returnStatus());在应该返回“A”但返回一个空字符串的网络类中。 – 2014-11-21 11:17:08

+0

现在这个工作是否与Enigmativity的答案? – 2014-11-21 12:01:28

+0

不,没有运气!这真的很奇怪,一切似乎都很好,但由于某种原因,它不起作用。我一次又一次地浏览代码,但仍然没有运气。 – 2014-11-21 12:03:08