2014-11-21 42 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