Click here to Skip to main content
15,904,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
namespace WindowsFormsApplication1
{
    class CheckUsername_Pass
    {
        public static string Check(string UsernameCheck, string PasswordCheck,string username, string password)
        {
            UsernameCheck = "Omega" + "omega";
            PasswordCheck = "Sweet12" + "sweet12";
 
            if (username == UsernameCheck & password == PasswordCheck)
            {
                System.Windows.Forms.MessageBox.Show("Login Successful");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Login Failed \n Please Check The Username Or Password For Errors.");
            }
        }
    }
}

I'm trying to practice classes and just can't seem to get this working. I just recently started programming in C# a few days ago and what this class is supposed to do is to check to see if the username and password is correct. If the username and password are correct then a message box will appear stating that it is indeed correct. Otherwise a message box will appear saying that the login failed.
Posted
Updated 9-Jun-11 7:20am
v3

So far, very bad. Let's see:

Method Check: you assign UsernameCheck and PasswordCheck to some immediate string constants. You should not use immediate constants as it's not supportable, but let's sat it's just to start the development. But at the moment of assignment that was variable holding the values of input parameters. By assigning them to something else you lost the values passed by the call.

Later on you perform the check. Use "&&" instead of "&". This is the same operation, but you should really use "&&" for optimization. As a Boolean operation, "&" should be used only when operands cause side effect, to avoid this side effect to be optimized out.

Now, as first two parameters of Check are ignored, the result of comparison depends only on username and password.

Always run code under debugger to see what's going on. The problems of unexpected behavior like this one will disappear. Just a note: you don't really practice OOP yet. There will be virtual functions, inheritance and late binding when real things start. You're confused with elementary things already. I would advice you to slow down and develop just simplest exercise, don't try to make real application — just yet. It will come soon.

Good luck,
—SA
 
Share this answer
 
Comments
RakeshMeena 9-Jun-11 3:11am    
My 5. Totally agree with you.
For all who face code issues, just try something yourself first because till you don't try, you are not going to learn/understand anything.
Sergey Alexandrovich Kryukov 9-Jun-11 10:15am    
Thank you, Rakesh.
You're right about trying and practicing. A lot of knowledge is purely illusionary, especially in the inexperienced. Understanding need validation...
--SA
Monjurul Habib 9-Jun-11 14:30pm    
nice advice, my 5.
Sergey Alexandrovich Kryukov 9-Jun-11 14:53pm    
Thank you, Monjurul.
--SA
I could try to solve this issue, but I think I better give you a reference where you can learn. Take a look here:

http://msdn.microsoft.com/en-us/vcsharp/aa336738[^]

There are lots of examples, tutorials, etc.
This will help you understand how things work in .NET


When reading your code I wonder what you are trying to do with these lines:

UsernameCheck = "Omega" + "omega";
PasswordCheck = "Sweet12" + "sweet12";


Perhaps your problem is solved when you delete these two lines.
 
Share this answer
 
Comments
Monjurul Habib 9-Jun-11 14:31pm    
nice links, my 5.
Gotta wonder why you're setting the check variables the way you are.

I think this has as much to do with learning the framework classes as anything else. If you set the check variables like so:

C#
string usernamecheck = "OMEGA";
string passwordcheck = "SWEE12";

and then do this comparison, you'll be gold:

C#
if (usernamecheck == username.ToUpper() && passwordcheck == password.ToUpper())
{
}


Also, in your original code snippet, your comparison is incorrect because you're using a single "&" character (see my example above for the correct usage).
 
Share this answer
 
Thanks guys. I appreciate your help :) I'll start smaller again and build up to this.
 
Share this answer
 
Comments
Monjurul Habib 9-Jun-11 14:20pm    
This can not be an answer.please click on "Add Comment", write your desire comment then "Submit Comment".

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