简体   繁体   English

为什么在使用“不等于”时,if运算符不起作用?

[英]Why doesn't or operator work in if statement when using “not equals”?

I am a bit confused by this. 我对此有些困惑。 I want to hide a button if the current username of the system is != Administrator OR Administrator2, but the only way to get my desired goal is by using the && instead of || 如果系统的当前用户名是!= Administrator或Administrator2,我想隐藏一个按钮,但是要达到我期望的目标的唯一方法是使用&&而不是|| .

 string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

        if
            (userName != @"PC\Administrator" && userName != @"PC\Administrator2")
        {              
            button2.Hide();
        }

However, in another spot in my form to open button2 , if I use == it then seems to work? 但是,在我的表单的另一个位置打开button2 ,如果我使用== ,那似乎可行吗?

  string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  var adminform = new AdminForm();

      if  (userName == @"PC\Administrator" || userName == @"PC\Administrator2")
        {
            adminform.Show();
        }

Any idea why I can't use || 知道为什么我不能使用|| when using != in the first example? 在第一个示例中使用!=时?

De Morgan's Laws for Boolean operations are an important skill for a programmer to understand intimately. De Morgan的布尔运算定律是程序员深入了解的一项重要技能。

In your case, when negating you need additional parentheses to correctly order the operations, or as in your case change the || 在您的情况下,否定时需要附加括号以正确地对操作进行排序,或者如您所愿,更改|| to an && . && To use the || 使用|| you'd put 你把

if (!(userName == @"PC\Administrator" || userName == @"PC\Administrator2"))

Enclose the entire not expression in parenthesis and negate that. 将整个not表达式括在括号中并取反。 Like this: 像这样:

if  (!(userName == @"PC\Administrator" || userName == @"PC\Administrator2"))
{
   button2.Hide();
}

It's the basic law of logic. 这是逻辑的基本定律。

De morgan's law 摩根定律

!(a && b) = !a || !b

Sometimes it's easier to write it in plain words to understand: 有时候,用通俗易懂的文字写起来更容易理解:

if username is not equal to administrator OR username is not equal to administrator2 then hide the admin form 如果username不等于administrator username不等于administrator2则隐藏管理表单

let's say that username is administrator 假设usernameadministrator

Then the control will hide , because one of things is true (it's not administrator2 ) 然后,该控件将隐藏 ,因为其中之一是正确的(不是administrator2

Why ? 为什么呢

question _____________________________________value 问题 _____________________________________值


if username is not equal to administrator _________0 如果username不等于administrator _________0

if username is not equal to administrator2 ________1 如果username不等于administrator2 ________1


Ok, so the result is 0 or 1 which is true 好的,所以结果是0 or 1 ,这是true

First of all, I dont really have experience with c#, but if I understood your problem correctly it is the logic you use and not ac#-only thing. 首先,我真的没有c#的使用经验,但是如果我正确理解了您的问题,那是您使用的逻辑,而不是仅用于c#的东西。

You want the method "hide" to be called when the user is not one of the two administrators, but the logic you tried was to get two boolean values: user_is_not_admin1 user_in_not_admin2 您希望当用户不是两个管理员之一时调用方法“ hide”,但是您尝试的逻辑是获取两个布尔值:user_is_not_admin1 user_in_not_admin2

When you use && you check that the user is not admin1 and not admin 2, which works as intended. 使用&&时,请检查该用户不是admin1还是admin 2,这可以按预期工作。 But when replacing it with || 但是,当用||替换它时 you would check that the user is never both admin1 and admin2 at the same time (which seems strange if it were to happen). 您将检查该用户永远不会同时使用admin1和admin2(如果发生这种情况,这似乎很奇怪)。 Instead, when attempting to use || 相反,当尝试使用||时 you also need to move the nagation. 您还需要移动命名。 What you want is to check that the user "is not (admin1 or admin2). 您要检查的是“不是”用户(admin1或admin2)。

For further reading, take a look at De Morgans Law. 为了进一步阅读,请看《 De Morgans Law》。 http://en.wikipedia.org/wiki/De_Morgan%27s_laws http://en.wikipedia.org/wiki/De_Morgan%27s_laws

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM