简体   繁体   中英

Correct use of logical operator in C#

I have the following If block that runs upon pressing a command button on a form object. 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 || 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 . ToString method returns string "System.Windows.Forms.TextBoxBase". This string is obviously never empty or 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. 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 . What you want is to check the contents of the Text property like so:

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

try this:

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

or maybe txtName is returning the TEXT object...try this then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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