【问题标题】:C# Boolean Evaluation Not Evaluating As ExpectedC# 布尔评估未按预期进行评估
【发布时间】:2015-02-06 01:08:14
【问题描述】:

我有一个 Visual Studio C# WinForms 应用程序,其中必须计算一个布尔值以确定程序接下来要做什么,

要么抛出一个消息框,要么执行一个函数。问题是当布尔值评估为

是的。

代码如下:

private void btnNextQuestion_Click(object sender, EventArgs e)
{
    if (QuestionNeedsSaving == true)
    {
        QuestionNeedsSaving = false;
        MessageBox.Show("You have made changes to this question." + "\r\n" + "\r\n" + "Click the Update Question button to " + "\r\n" + "Save changes or the changes will be lost.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else if (QuestionNeedsSaving == false)
        GoToNextQuestion();
}

如果布尔值“QuestionNeedsSaving”为真,则它被设置为假并抛出一个消息框。 否则,如果“QuestionNeedsSaving”为假,则调用“GoToNextQuestion”函数。

问题在于,如果“QuestionNeedsSaving”为真,则消息框和“GoToNextQuestion”都会被执行。

“GoToNextQuestion”只有在“QuestionNeedsSaving”一开始就为假时才应该执行。

【问题讨论】:

  • 听起来该方法被调用了两次。设置断点并使用 Visual Studio 单步执行。
  • 代码没问题,错误应该在此处未显示的部分 - 即按钮单击的双重注册。

标签: c# boolean


【解决方案1】:

实际上主要问题是 QuestionNeedsSaving 的恒定状态,它总是通过 if 条件并更好地在其他地方做出 QuestionNeedsSaving 值的决定:

    private void btnNextQuestion_Click(object sender, EventArgs e)
    {
        if (QuestionNeedsSaving == true)
        {
            MessageBox.Show("You have made changes to this question." + "\r\n" + "\r\n" + "Click the Update Question button to " + "\r\n" + "Save changes or the changes will be lost.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else if (QuestionNeedsSaving == false)
            GoToNextQuestion();
    }

【讨论】:

  • 不,没关系。
  • 这无关紧要,并且 QuestionNeedsSaving 变量的声明超出了 btnNextQuestion_Click 的范围,这意味着当程序在单击按钮后运行该值时,该值将为 false,并且当您单击再次按钮 否则 QuestionNeedsSaving 在此处未提及的其他位置更改其值。
  • QuestionNeedsSaving 是一个全局变量。当文本框的 TextChanged 事件触发时,QuestionNeedsSaving 设置为 true,否则设置为 false。问题是,无论 QuestionNeedsSaving 是真还是假,我在上面发布的代码中的两个条件语句都会触发。两个代码块都会触发,而不仅仅是一个块或另一个。
【解决方案2】:

只需运行此代码,您就会知道,如果出现奇数按钮单击 QuestionNeedSaving=true 会给出下一个问题消息,但如果偶数按钮单击 QuestionNeedSaving=false 会给出 OOPS!消息,并且永远不会相同。

using System;
using System.Drawing;
using System.Windows.Forms;
namespace boolDecission
{
    public partial class Form1 : Form
  {
    bool QuestionNeedsSaving = false;
    int x = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnNextQuestion_Click(object sender, EventArgs e)
    {
        x = x + 1;
        //even number will make QuestionNeedsSaving = false and odd            number will make QuestionNeedsSaving = true. 
        if (x % 2 == 1)
        {
            QuestionNeedsSaving = false;
        }
        else 
        {
            QuestionNeedsSaving = true;
        }
        if (QuestionNeedsSaving == true)
        {
            MessageBox.Show("You have made changes to this question." + "\r\n" + "\r\n" + "Click the Update Question button to " + "\r\n" + "Save changes or the changes will be lost.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else if (QuestionNeedsSaving == false)
        {
            GoToNextQuestion();
        }
    }
    public void GoToNextQuestion()
       {
            MessageBox.Show("Next Question");
       }

    }
}

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 2014-04-15
    • 1970-01-01
    • 2014-05-29
    • 2020-12-17
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多