Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
pls check the coding
of if condition
and tell the error
C#
private void OK_Click(object sender, EventArgs e)
       {
            if(UsernameTextBox.Text ="Manager" & PasswordTextBox.Text = "Maintenance")
           {
           frmMDI m=new frmMDI();
           m.Show();
           }
            else
           MessageBox.Show("Unauthorised User", "Authentication");
           UsernameTextBox.Focus();
           UsernameTextBox.SelectionStart = 0;
       }

thanx in advance
regards
Posted

Change his line:
C#
if(UsernameTextBox.Text ="Manager" & PasswordTextBox.Text = "Maintenance")

to
C#
if(UsernameTextBox.Text == "Manager" && PasswordTextBox.Text == "Maintenance")
 
Share this answer
 
v2
Comments
Kim Togo 17-May-11 10:33am    
Correct my 5 :-)
Ankur\m/ 18-May-11 0:35am    
Thanks, Kim!
Sergey Alexandrovich Kryukov 17-May-11 18:46pm    
Agree, 5.
--SA
Ankur\m/ 18-May-11 0:36am    
Thanks, SA!
C#
private void OK_Click(object sender, EventArgs e)
       {
            if(UsernameTextBox.Text =="Manager" && PasswordTextBox.Text == "Maintenance")
           {
           frmMDI m=new frmMDI();
           m.Show();
           }
            else
           MessageBox.Show("Unauthorised User", "Authentication");
           UsernameTextBox.Focus();
           UsernameTextBox.SelectionStart = 0;
       }
 
Share this answer
 
v2
Comments
Kim Togo 17-May-11 10:32am    
Correct, my 5
Sergey Alexandrovich Kryukov 17-May-11 18:47pm    
Agree, 5.
--SA
jsc42 25-May-11 12:56pm    
Many style gurus suggest comparing strings against variables, instead of comparing variables against strings just so that compilers would catch the mistaken use of '=' instead of '=='. Viz:

if ("Manager" == UsernameTextBox.Text && "Maintenance" == PasswordTextBox.Text)

which is valid whereas

if ("Manager" = UsernameTextBox.Text && "Maintenance" = PasswordTextBox.Text)

is invalid.

Personnaly, I think it looks clunky and awkward. Anyone who uses that construct is already aware of the issue of confusing '==' and '=', and hence probably does not need this backward construct. Conversely, people who are likely to fall foul of '=' instead of '==' are unlikely to be ones who would use the backwards construct. But I am sure there must be a set of people somewhere that would benefit fropm this backwards construct.
You are using the assignment = instead of comparison ==

if(UsernameTextBox.Text == "Manager" & PasswordTextBox.Text == "Maintenance")
 
Share this answer
 
Comments
Kim Togo 17-May-11 10:32am    
Almost correct. The bit operator & has to be change to &&
Jeremy Hutchinson 17-May-11 11:14am    
While I generally agree it is good practice to use && over & where appropriate (there's places where it's not), it is certainly not required. In a case like this were you are just comparing strings it is not going to have any noticeable effect on performance and amounts to a style issue.

If I were going to correct a style issue, the first thing I would change is to put braces around the else code to make it clear that the Focus and selection setting happen for both paths, which may or may not be what was intended.
Kim Togo 17-May-11 13:03pm    
I just prefer to use "&&" operator, it only evaluate second expression it the first is true.
Then I can write code like

if (this.Name != null && this.Name.Contains("Hello"))
{
}

But you are right, there is no performance degree in this code :-)
Sergey Alexandrovich Kryukov 17-May-11 18:48pm    
Many thins are technically not required, but absolutely required to make a good call.
Wrong excuse.
--SA
This line is wrong:
C#
if(UsernameTextBox.Text ="Manager" & PasswordTextBox.Text = "Maintenance")

It should read:
C#
if(UsernameTextBox.Text == "Manager" & PasswordTextBox.Text == "Maintenance")


In C# you use = operator to assign and == to compare.
 
Share this answer
 
Comments
Kim Togo 17-May-11 10:30am    
Almost correct. The bit operator & has to be change to &&
Fabio V Silva 17-May-11 10:35am    
Why?
Kim Togo 17-May-11 13:02pm    
Sorry. Of cause you can do it like that. :-)
Using one "&" operator just evaluates all expressions.

I just prefer to use "&&" operator, it only evaluate second expression it the first is true.
Then I can write code like

if (this.Name != null && this.Name.Contains("Hello"))
{
}
Sergey Alexandrovich Kryukov 17-May-11 18:49pm    
Because. Kim is absolutely right. It's a mistake, even though it would work the same way.
--SA
Fabio V Silva 18-May-11 14:57pm    
"Because"?! That's a good answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900