【发布时间】:2015-01-26 13:09:01
【问题描述】:
我有三个类,下面提供代码。
网络 - 添加和删除电话,处理呼叫。 Phone1和Phone2加入网络后可以互相通话。
但是当我将手机连接到网络并尝试将 phone1 呼叫到 phone2 时,我遇到了问题;它一直让我“接收器忙”。从phone1调用时,我尝试进行一些调试并读取phone2的状态,但它返回一个空字符串(实际上应该返回“A”,当它被添加到网络时,因为我将其值设置为“A ")。
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";
}
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");
}
}
}
---------Phone1 类---------
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 类-------------
public partial class phone2 : phone1
{
public phone2()
{
InitializeComponent();
}
}
【问题讨论】:
-
我编辑了问题以清理代码,但进一步编辑它以删除与问题无关的任何代码会对您有所帮助。大多数人不愿意深入研究整个程序来找出问题所在。见How to create a Minimal, Complete, and Verifiable example
-
您希望
p2.Status在创建它和显示状态之间的 3 行中设置在哪里?构造函数没有设置它。您没有在makeCall中明确设置它。