Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all

look for the following function

C#
 public bool CheckPassword( string InputPass)

{

 if (InputPass==Password ) 
          return true ;
        else
          return false;

}


when i want to use the above func.

i have to write an IF statement
for example

void LoginBtn_Clicked()
{

    if ( !CheckPassword("****")) return ;

 // code for case the pass is right
.......


}


NOW my ques. i want CheckPassword func. to return void and not bool and to be used without any extra code to be like the following :



C#
void LoginBtn_Clicked()
{

   CheckPassword("xxx");   / i want the func. to be asif it is a gate or a firewall

 // code for case the pass is right
.......


}
Posted
Comments
LanFanNinja 24-Nov-11 8:39am    
What code is being called from LoginBtn_Clicked() if the password is correct?
Nickos_me 24-Nov-11 10:10am    
Remember for all of this solutions - store password in string class nat ideal idea.

C#
public bool CheckPassword( string InputPass)
 
{
 
 if (InputPass==Password ) 
{
          form2 obj= new form2();
          obj.show();
}
        else
{
         textbox1.focus();
messagebox.show("mismatch ");
          return;
}
 
}
 
Share this answer
 
v2
Comments
RaisKazi 24-Nov-11 8:32am    
Edited: Added "pre" tag.
If I understand you correctly, and you want to exit from LoginBtn_Clicked without having to check the return value of CheckPassword and 'returning' - well, you can't.

You could do what you originally had, or this:

C#
void LoginBtn_Clicked()
{
   if (CheckPassword("xxx"))
   {
       // stuff in here
   }
}


Or, you could get CheckPassword to throw an exception, but that's an awful awful solution. Why do you want it to be void? It absolutely should return a bool, because the password check either fails or succeeds.
 
Share this answer
 
But why? The result returned by the CheckPassword method should branch the code into one of two directions. Change the method to not return a bool is not only pointless, but is also ill-advised, not to mention adding a certain level of ambiguity to the code.
 
Share this answer
 
If you want that the Function should return void then this is the method not a function beacause function always return a value.
C#
void CheckPassword(string inputPass)
        {
            if (passWord != inputPass)
                {
                 //Your Code if password is correct
                }
            else
                {
                 //Your Code if Password is wrong
                }
        }

Call CheckPassword on click of Login button.
C#
void btnLogin_Click()
       {
           CheckPassword("xxxxxxxx");
       }
 
Share this answer
 
You could just use the CheckPassword function to exit if not true, else the app can carry on like this

C#
void CheckPass(string inputPass)
        {
            if (passWord != inputPass)
                Application.Exit;
        }

        void btnLogin_Click()
        {
            CheckPass("XXX");
            //Carry on if password correct
        }
 
Share this answer
 
Comments
AhmedOsamaMoh 24-Nov-11 8:28am    
this will close the whole software !!!
Wayne Gaylard 24-Nov-11 8:31am    
What do you want it to do?

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