简体   繁体   English

在C#中正确使用逻辑运算符

[英]Correct use of logical operator in C#

I have the following If block that runs upon pressing a command button on a form object. 我有以下If块,它在按下表单对象上的命令按钮时运行。 This should simply check to see if any of the four mentioned text boxes are empty and if so, display a message box then exit that procedure so that the user can correct the fields and continue. 这应该仅检查一下上述四个文本框是否为空,如果显示为空,则显示一个消息框,然后退出该过程,以便用户可以更正字段并继续。

Here is the relevant code: 以下是相关代码:

 if (string.IsNullOrWhiteSpace(txtName.ToString()) || 
     string.IsNullOrWhiteSpace(txtID.ToString()) ||
     string.IsNullOrWhiteSpace(txtSalary.ToString()) ||
     string.IsNullOrWhiteSpace(txtERR.ToString()))
 {
  MessageBox.Show("One or more text fields are empty or hold invalid data, please correct this to continue","Data Error",MessageBoxButtons.OK);
  return;
 }

I've left all the text fields blank, and even tried putting white space characters in but the conditional code isn't being executed. 我将所有文本字段留为空白,甚至尝试在其中添加空格字符,但条件代码未执行。 As the code isn't executing I'm assuming there is something wrong with my if statement, perhaps I'm not using the 'or' operator || 由于代码未执行,因此我假设我的if语句出了点问题,也许我未使用'or'运算符|| correctly? 正确吗? Any help appreciated. 任何帮助表示赞赏。

If you are checking textboxes you need to get the text from the textbox. 如果要检查文本框,则需要从文本框中获取文本。

 if (string.IsNullOrWhiteSpace(txtName.Text) || ... 


As a little bonus you could also write it like this: 作为一点好处,您也可以这样写:

if(new [] {txtName, txtID, txtSalary, txtERR}
  .Any(tb => string.IsNullOrWhiteSpace(tb.Text)))
{
   MessageBox.Show("One or more text fields are empty or hold invalid data, please correct this to continue","Data Error",MessageBoxButtons.OK);
   return;
}

You should use Text property of TextBox . 您应该使用TextBox Text属性。 ToString method returns string "System.Windows.Forms.TextBoxBase". ToString方法返回字符串“ System.Windows.Forms.TextBoxBase”。 This string is obviously never empty or null. 该字符串显然绝不能为空或为null。

if (string.IsNullOrWhiteSpace(txtName.Text) || 
 string.IsNullOrWhiteSpace(txtID.Text) ||
 string.IsNullOrWhiteSpace(txtSalary.Text) ||
 string.IsNullOrWhiteSpace(txtERR.Text)) 
 {
  MessageBox.Show("One or more text fields are empty or hold invalid data, please correct this to continue","Data Error",MessageBoxButtons.OK);
  return;
 }

If txtName, txtID etc. name of controls then you need to refer to .Text property. 如果控件的txtName,txtID等名称,则需要引用.Text属性。 Try something like snippet below: 尝试以下类似代码段的方法:

if (string.IsNullOrWhiteSpace(txtName.Text) || 
     string.IsNullOrWhiteSpace(txtID.Text) ||
     string.IsNullOrWhiteSpace(txtSalary.Text) ||
     string.IsNullOrWhiteSpace(txtERR.Text))
 {
  MessageBox.Show("One or more text fields are empty or hold invalid data, please correct this to continue","Data Error",MessageBoxButtons.OK);
  return;
 }

TextBox.ToString() will return the type of the TextBox - thus this will never be NullOrWhiteSpace . TextBox.ToString()将返回TextBox的类型-因此,它永远不会是NullOrWhiteSpace What you want is to check the contents of the Text property like so: 您想要的是检查Text属性的内容,如下所示:

if (string.IsNullOrWhiteSpace(txtName.Text || 
 string.IsNullOrWhiteSpace(txtID.Text) ||
 string.IsNullOrWhiteSpace(txtSalary.Text) ||
 string.IsNullOrWhiteSpace(txtERR.Text))
 {
      MessageBox.Show("One or more text fields are empty or hold invalid data, please correct this to continue","Data Error",MessageBoxButtons.OK);
      return;
 }

I don't use IsNullOrWhiteSpace for that kind of test, instead i prefer to use IsNullOrEmpty 我不使用IsNullOrWhiteSpace进行这种测试,相反,我更喜欢使用IsNullOrEmpty

try this: 尝试这个:

if (string.IsNullOrEmpty(txtName.Text)||...)
{...

or maybe txtName is returning the TEXT object...try this then 或者txtName返回TEXT对象...然后尝试

if (string.IsNullOrEmpty(txtName.Text.toString())||...)
{...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM