【问题标题】:Is there something wrong with my If Statements?我的 If 语句有问题吗?
【发布时间】: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


【解决方案1】:

如果转换失败,TryParse 会将 Result 设置为零。由于您不断调用 TryParse,因此您不断重置 Result。

如果你只想检查解析错误,这应该可以工作:

ContainsInvalidEntry = false;
ContainsInvalidEntry |= !int.TryParse(P1STRtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1DEXtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1VIGtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1RMtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1BMtextbox.Text, out result));
return ContainsInvalidEntry;

旁白:将布尔值与真假进行比较有点(请原谅双关语。)奇怪。 if ( ( ParsedOkay == ( false ) ) ) 可能有效,但if ( !ParsedOkay ) 更常见。

【讨论】:

  • 我应该尝试在每个处理失败的 if 语句中将结果设置回 1 吗?
  • 我从未见过这样的代码结构。请你简要解释一下你在这里做什么。我的意思是我明白了它的要点。似乎是一种更简洁、更有效的方式来做我想做的事情。
  • @user3134679 - 代码假定没有无效条目。然后,对于要解析的每个字段,它执行否定的 TryParse (!) 与当前状态的逻辑 OR。因此,任何失败的 TryParse 都会将 ContainsInvalidEntry 设置为 true。或者,您可以简单地return !( int.TryParse( Thing1, out DontCare ) && int.TryParse( Thing2, out DontCare ) && ... );。如果任何 TryParse 调用失败,则由于逻辑 NOT (!),它将返回 true。
【解决方案2】:

我对您的代码感到困惑。但我认为这会对你有所帮助。如果我说得对,您想检查文本框中的所有文本是否都可以解析为 int。如果是这样,您想打印出“成功”;

private bool isParsable(TextBox t) //takes a TextBox as paramater and returns true if
    {                              // its parsable
        int i = 4;
        if (int.TryParse(t.Text, out i) == true)
            return true;
        else
            return false;            
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if(isParsable(tbox1) == true && isParsable(tbox2) == true) //if every textbox
        {                                                          //is parsable print
            tblock1.Text = "succes";
        }
        else
        {
            tblock1.Text = "error";
        }
    }

【讨论】:

  • 你明白了。非常感谢我想要我的程序做的事情。我马上去试试
  • int i = 4的意义是什么;你怎么给它那个值?
  • 'code'if (isParsable(P1STRtextbox) == true && isParsable(P1STRtextbox) == true && isParsable(P1VIGtextbox) == true && isParsable(P1RMtextbox) == true && isParsable(P1BMtextbox) = = true)'code' 我总共有 5 个文本框要解析。想知道 int i = 4 是否会影响这一点。
  • “int i = 4”是因为你需要一个整数作为out来调用函数。
  • 我是最后一个尝试使用一些奇怪的技巧来保存击键的人,但我没有按英镑支付代码费用。写{ if ( something == (true) ) { return ( true ); ) else { { { return ( ( ( false ) ) );;; } } } } 而不是return something; 相当笨拙。好的一面是它允许您在一个返回或另一个返回上设置一个断点。缺点是它让你看起来不相信真相。您可能还想习惯条件运算符 (?:),例如foo.Text = Valid( bar ) ? "success" : "failure";.
【解决方案3】:

首先你不需要像这样写你的 if 语句,它太杂乱而且没必要。复杂的代码总是容易出错,

而不是这个:

 if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)

你可以写:

if(!int.TryParse(P1STRtextbox.Text, out result))

因为 TryParse 已经返回布尔结果。如果您想检查它是否为假,只需将否定运算符 (!) 放在语句的开头。您还可以编写一个简单的方法来检查您的文本是否可解析:

static bool CheckForParse(params string[] values) 
{
   int x;
   if(values.Lenght > 0)
   {
       for(int i=0; i<values.Lenght;i++)
       {
          if(!int.TryParse(values[i], x)) return false;
       }
       return true;

    } else { return false }

    return false;
}

你可以这样称呼它:

bool result = CheckForParse(P1STRtextbox.Text,
                            P1DEXtextbox.Text,
                            P1VIGtextbox.Text,
                            P1RMtextbox.Text,
                            P1BMtextbox.Text);
if(result)
{
   P1STRtextbox_TextChanged(sender, e);
   P1DEXtextbox_TextChanged(sender, e);
   ...
}

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 2015-09-15
    • 1970-01-01
    • 2017-02-11
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多