开发者

C# simple IF OR question

开发者 https://www.devze.com 2022-12-31 15:57 出处:网络
Sorry to ask this as I thought I knew the answer, I want to exit the program if userName is greater than 4 characters or userName is not an account called student. However this even 开发者_如何转开发i

Sorry to ask this as I thought I knew the answer, I want to exit the program if userName is greater than 4 characters or userName is not an account called student. However this even 开发者_如何转开发if the userName is only 3 characters and is not student I'm still hitting Application.Exit. What am I doing wrong?

if (userName.Length > 4 | userName != "student")
{
    Application.Exit();
}

Shame on me :-(


While you should use || instead of |, they will give the same result in this situation. Despite the upvoting of the other answers, changing | to || will not solve your problem.

Your real problem is that the conditions you want to check for will always be true. Either your userName is not student, or it is student and then it is also longer than 4 characters.

When you have a username that is only 3 characters it is not equal to student, therefore the program quits.

From your description of what you expect, I think you mean this:

if (userName.Length > 4 && userName != "student")
{
    Application.Exit();
}


You need to use the boolean OR (||) operator instead of the bitwise OR (|)

As I said in my comments though, your logic doesn't necessarily make any sense to me. The way it is writting, the statement will always be true:

  • If userName is not student, the statement is true and the application exits.

  • If userName is student, then length > 4 and the statement is true again (which causes an exit).

You could change things to:

if(username.Length > 4 && userName != "student")
{
    Application.Exit();
}

Which makes more sense logically, but since I don't know your intent I can't guarantee that it would work the way you want it to.


From looking at your requirement your check for the length of chars is negligable or at least you haven't mentioned the reason why you want to check length of the char. From the example you have provided I would simply check if (userName != "student") I don't see the need for the extra check this is something that can be forced in the UI.


If I am not wrong, you are trying to enter into the if loop when either of the condition is true. i.e., enter into the if loop

  1. when Length is greater than .
  2. when userName should not be equal to "student"

condition like username = "ABC" is not "student", your condition still right and will enter. It will execute still when userName is equal = "student";

Here You should use AND operator than just OR operator.

if (userName.Length > 4 & userName != "student")
  {
   Application.Exit();                
  }

You can also achieve the same result with && operator. & opertor is same as && operator. when X = false and Y is true; Y will not be evaluated at all. Since X is already false. this is method is also call short-circuit evaluation.


 if (userName.Length > 4 || userName.ToLower() != "student")
 {
     Application.Exit();
 }

Try this.

0

精彩评论

暂无评论...
验证码 换一张
取 消