C中的'和or'运算符 [英] 'and or' operator in C#

查看:148
本文介绍了C中的'和or'运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#在Web应用程序中创建一个登录表单,我想知道如果一个或两个文本框都为空,是否有一种更简单的方法可以禁用登录按钮。如果两个或一个都为空,我希望它禁用,如果它们都已填充,则启用它。是否有一种'和or'运算符,我可以使用而不是一个用于用户名的循环和一个用于两者的textchanged事件中的密码。

赞:

  if (username.Text ==   ' 和/或' password.Password ==  
{
login.IsEnabled = false ;
}



有没有办法做到这一点?谢谢

解决方案

这是OR。或者结束。此外,使用

  if (someBooleanExpression)
someBooleanProperty = ;
else
someBooleanProperty = false ;



将是愚蠢的,因为它严格等同于

 someBooleanProperty = someBooleanExpression; 





另外,你不应该使用并避免使用任何立即常量,除了大多数基本行0,1或null。请改用 string.Empty



所以,你需要

 login.IsEnabled = username.Text!=  string  .Empty&& password.Password!=  string  .Empty; 



注意使用'&&'而不是'& ;。这消除了冗余检查,以及||。请注意,如果涉及呼叫,它也可以消除检查的副作用,这就是为什么'&'和'||'不会从操作员集中删除。



在实践中,建议进行更严格的检查:

 login.IsEnabled = username.Text.Trim()!=  string  .Empty&& password.Password.Trim()!=  string  .Empty; 





-SA


没有and or这样的东西。在您的情况下,这只是一个OR表达式。我想你最好看一下布尔逻辑真值表。


这很简单,你需要检查其中一个,你不需要使用两个if表达式,所以你的代码可以是:



  if  string  .IsNullOrEmpty(username.Text)||  string  .IsNullOrEmpty(password.Text))
{
login.IsEnabled = false ;
}





[edit]删除不必要的代码块[/ edit] < /小>


I'm trying to make a login form in a web application in C# and I was wondering if there was an easier way to make the login button disabled if one or both text boxes are empty. I want it to disable if both or one are empty and enable if they are both populated. Is there a kind of 'and or' operator that I can use instead of doing one if loop for the username and one for the password in the textchanged event of both.
Like:

if (username.Text == "" 'and or' password.Password == "")
{
    login.IsEnabled = false;
}


Is there any way of doing this? Thank you

解决方案

This is OR. Or END. Besides, using

if (someBooleanExpression)
   someBooleanProperty = true;
else
   someBooleanProperty = false;


would be plain silly, as it is strictly equivalent to

someBooleanProperty = someBooleanExpression;



Also, you should not use "" and avoid using any immediate constants except most fundamental ones line 0, 1 or null. Use string.Empty instead.

So, you would need

login.IsEnabled = username.Text != string.Empty && password.Password != string.Empty;


Note using '&&' instead of '&'. This eliminates redundant checks, as well as '||'. Note that it can also eliminate side effect of the check if a call is involved, that's why '&' and '||' are not removed from the operator set.

In practice, it's advised to make some stricter check:

login.IsEnabled = username.Text.Trim() != string.Empty && password.Password.Trim() != string.Empty;



—SA


There's no such thing as "and or". In your case, this would just be an OR expression. I think you better have a look at the boolean logic truth tables.


It's very simple, just you need to check one of them and you don't need to use two if expression so your code can be:

if (string.IsNullOrEmpty(username.Text) || string.IsNullOrEmpty(password.Text))
{
    login.IsEnabled = false;
}



[edit]unnecesary code block deleted[/edit]


这篇关于C中的'和or'运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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