【发布时间】:2014-01-23 03:37:29
【问题描述】:
此函数检查文本框中的值是否可解析。此方法在 this 下面的方法中被调用。
private bool CheckForInvalidEntries()
{
bool ParseIsSuccessfull; int result; //These 2 variables are for trying to parse the entries in the Stat text boxes
bool ContainsInvalidEntry = false;
if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1DEXtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1VIGtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1RMtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1BMtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else ContainsInvalidEntry = false;
return ContainsInvalidEntry;
}
这个函数是进程统计点按钮被点击的事件
private void p1ProcessPointsBtn_Click(object sender, EventArgs e)
{
bool EntriesAreInvalid = new bool();
EntriesAreInvalid = CheckForInvalidEntries();
if (EntriesAreInvalid == true)
{
P1STRtextbox_TextChanged(sender, e);
P1DEXtextbox_TextChanged(sender, e);
P1VIGtextbox_TextChanged(sender, e);
P1RMtextbox_TextChanged(sender, e);
P1BMtextbox_TextChanged(sender, e);
}
else
{
MessageBox.Show("Success");
}
FUNCTIONALITY:当用户按下“Process Stat Points”按钮时,程序会检查5个文本框中的条目是否能够被解析(在CheckForInvalidEntries方法中)。然后它返回一个布尔值给 EntriesAreInvalid 变量(在 p1ProcessPointsBtn_Click 方法中)。如果条目不可解析,则执行操作 A,如果条目可解析,则执行操作 B。
问题:如果所有文本框中的数字都是可解析的,我不会得到结果。如果文本框不可解析,我只会得到结果。我认为这与“CheckForInvalidEntries”方法中的 if 语句有关。我能做些什么来解决我的问题。非常感谢您的时间和精力!
【问题讨论】:
-
删除代码中的
= new bool()部分。我闻起来很时髦。 -
整个方法闻起来很时髦
-
哈哈,我知道问题出在 CheckForInvalidEntries 方法中,它太时髦了。我只是觉得我采取的逻辑方法不好。即使是合乎逻辑的建议也会很好:)
-
我认为该方法没有问题。您能否调试代码并在代码中找到应用程序状态与您预期不同的点?
-
如果我不得不猜测,你必须向后逻辑。您的方法将在第一个 valid 条目上停止并返回 true;听起来您希望它在第一个 invalid 条目上停止并返回 false?
标签: c# if-statement boolean logic boolean-logic