【问题标题】:Unable to use logical operator within conditional statement [closed]无法在条件语句中使用逻辑运算符 [关闭]
【发布时间】:2018-09-26 09:02:04
【问题描述】:

我正在尝试编写一个条件语句,该语句将在单击按钮时进行检查,以检查是否至少选中了一个复选框。 所以一个例子是:

if (
    checkbox_delete.Checked = false && 
    checkbox_export = false && 
    checkbox_name = false && 
    checkbox_PST = false
)
{
    string messageboxtext = "Please check at least one of the checkboxes.";
    MessageBox.Show(messageboxtext);
}

我收到错误消息说: 运算符“&&”不能应用于“bool”和“System.Windows.Forms.CheckBox”类型的操作数
谁能帮忙弄清楚我做错了什么?

附言。我也试过做

 if (
    (checkbox_delete.Checked = false) && 
    (checkbox_export = false) && 
    (checkbox_name = false) && 
    (checkbox_PST = false)
 )
 {
     string messageboxtext = "Please check at least one of the checkboxes.";
     MessageBox.Show(messageboxtext);
 }

然后我得到:

无法将类型“bool”隐式转换为“System.Windows.Forms.CheckBox”

【问题讨论】:

  • 我忘了在其他检查中添加“.checked”。
  • 这是否意味着您解决了您的问题,和/或您需要编辑您的帖子?
  • 我解决了这个问题 - 谢谢。
  • 您不能将整个对象设置为 false,您需要使用 "==" insead of "="
  • 由于一个或多个简单的印刷错误,我投票决定关闭它。

标签: c# conditional-statements operator-keyword logical-operators


【解决方案1】:

您当前的代码存在几个问题:

  1. 如您所见,您需要引用属性Checked。相关文档请参见https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked(v=vs.110).aspx

  2. 另外,您应该使用==,而不是使用=。第一个用于赋值;第二个用于平等比较。有关这一点的更多信息,请参阅https://stackoverflow.com/a/4704401/361842。尝试在控制台应用程序或 LinqPad 中运行以下代码,以查看在被误解时可能会得到的一些奇怪结果:

问题示例

bool something = true;
Debug.WriteLine(something); //returns true
if (something = false) {
    Debug.WriteLine("this won't show");
} else {
    Debug.WriteLine("this will show");  //this is output, which you'd maybe expect, but for the wrong reasons... 
}
Debug.WriteLine(something); //returns false, as we've reassigned it accidentally above

if (something = false) {
    Debug.WriteLine("this won't run");
} else {
    Debug.WriteLine("surprise!"); //even though we saw above that something is now false, because we assign the value false to something and then evaluate the value of something (rather than comparing `something` with `false`, finding them equanlt and thus returning `true`; so the above statement effectively reads `if (false)`
}

输出:

True
this will show
False
surprise!

...除此之外,您当前的代码还将取消选中所有复选框,因为您将选中的属性设置为 false。

修改代码:

if (
    checkbox_delete.Checked == false && 
    checkbox_export.Checked  == false && 
    checkbox_name.Checked  == false && 
    checkbox_PST.Checked  == false
)
{
    string messageboxtext = "Please check at least one of the checkboxes.";
    MessageBox.Show(messageboxtext);
}

说“如果一切都是假的”的另一种方法是说“如果没有一个是真的”。您可能会发现这更容易阅读;但这取决于个人喜好。请参阅下面的示例:

if (!(
    checkbox_delete.Checked || 
    checkbox_export.Checked || 
    checkbox_name.Checked || 
    checkbox_PST.Checked 
))
{
    string messageboxtext = "Please check at least one of the checkboxes.";
    MessageBox.Show(messageboxtext);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2016-11-20
    • 2017-12-04
    • 2015-04-03
    相关资源
    最近更新 更多