-2

My if below for some reason checks for the first but not the second condition.

The first it checks the user/passwd against a db and the second is a hard code user and passowrd.

When I enter the hard coded user/passwd, it doesn't let me thru, only using the db user/passwd.

Any idea what is wrong?

if ((userTxt == userDB && passwdTxt == passwdDB) ||
    (userTxt == "user" && passwdTxt == "test"))
{
    switch (frmMdiMain.loginPageText)
    {
        case "exit":
            mainPage.Hide();
            Application.Exit();
            break;
        case "internal":
            mdiInternalUse internUseForm = new mdiInternalUse();
            internUseForm.Show();
            this.Close();
            break;
        default:
            break;
    }
}
ClickRick
  • 1,553
  • 2
  • 17
  • 37
ithieme1
  • 45
  • 3
  • 9
  • 4
    Please add the relevant language tag to your question. – Oliver Charlesworth May 19 '12 at 16:58
  • Is it possible that the input method you use give a `userTxt` and/or `passwdTxt` containing a newline/return character at the end? – Junuxx May 19 '12 at 17:01
  • I would suggest more context as well. What are the types of these values? The switch in the middle can be removed as it isn't relevant to your question. – Greg May 19 '12 at 17:02
  • 2
    If it is Java (as it looks) try `"user".equals(userTxt)&&"test".equals(passwdTxt)` (see *[How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)*). – Howard May 19 '12 at 17:02

1 Answers1

0

Without the language you are using it is hard to answer, but I think the problem is that the value of userTxt and passwdTxt are compared to a different object than the one represented by "user" and "test"

For example in C/C++ of userTxt is a pointer to char, it will have a different addess than `"user".

In java, the String object holding the value of userTxt and "user" can also be different.

You will need to perform a proper comparison on the strings (e.g. strcmp() in C, use sts::string in C++, use userTxt.compare("user") in java)

Attila
  • 28,265
  • 3
  • 46
  • 55