SierraOscar
SierraOscar

Reputation: 17637

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.

Upvotes: 0

Views: 422

Answers (5)

Liev04
Liev04

Reputation: 441

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())||...)
{...

Upvotes: -1

Magnus
Magnus

Reputation: 46947

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;
}

Upvotes: 10

Jurgen Camilleri
Jurgen Camilleri

Reputation: 3589

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;
 }

Upvotes: 1

Adil
Adil

Reputation: 3268

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;
 }

Upvotes: 2

Marko Juvančič
Marko Juvančič

Reputation: 5890

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;
 }

Upvotes: 3

Related Questions