如何在C#中实现此功能 [英] How to make this function in c#

查看:90
本文介绍了如何在C#中实现此功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人

寻找以下功能

Dear all

look for the following function

 public bool CheckPassword( string InputPass)

{

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

}



当我想使用上述功能时.

我必须写一个IF语句
例如



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
.......


}



现在我的问题.我想要CheckPassword功能.返回void而不是bool,并且无需任何额外的代码即可使用,例如:





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 :



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
.......


}

推荐答案

public bool CheckPassword( string InputPass)
 
{
 
 if (InputPass==Password ) 
{
          form2 obj= new form2();
          obj.show();
}
        else
{
         textbox1.focus();
messagebox.show("mismatch ");
          return;
}
 
}


如果我对您的理解正确,并且希望退出LoginBtn_Clicked,而不必检查CheckPassword的返回值和"returning",那么,您可以't.

您可以执行原先的操作,或执行以下操作:

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:

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



或者,您可以让CheckPassword引发异常,但这是一个糟糕的解决方案.您为什么要使它无效?绝对应该返回布尔值,因为密码检查失败或成功.



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.


但是为什么呢? CheckPassword方法返回的结果应将代码分为两个方向之一.更改方法以不返回bool不仅是毫无意义的,而且还是不明智的,更不用说在代码中添加一定程度的歧义了.
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.


这篇关于如何在C#中实现此功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆