Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# simple IF OR question

Tags:

c#

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 :-(

like image 620
Jamie Avatar asked May 24 '10 12:05

Jamie


1 Answers

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();
}
like image 88
Mark Byers Avatar answered Oct 23 '22 15:10

Mark Byers