Farid-ur-Rahman
Farid-ur-Rahman

Reputation: 1869

Checking multiple conditions in a single If statement - C#

I have a form in which there are many textBoxes from which three textboxes are required to fill in order to submit the form. I dont want to use each If block for each text box. Is there any way to use a single if statement for all the three text boxes? I am using the following code:

if (textBox1.Text != "" || textBox2.Text != "" || textBox4.Text != "")
{
   // Code
}
else
{
   MessageBox.Show("Fill required fields");
}

but this code works even a single text fox is filled and the rest of the required text boxes are empty.

Upvotes: 2

Views: 12909

Answers (7)

Amar Palsapure
Amar Palsapure

Reputation: 9680

Instead of using OR (||) use AND (&) in your condition.

Suggestion

  • Use Trim function from string to remove any white spaces from textbox (if required)
  • Instead of comparing like textBox1.Text != "" do String.IsNullOrEmpty(textBox1.Text) == false

Upvotes: 1

Jayy
Jayy

Reputation: 2436

Assuming you have 4 textboxes in total, below code will work

if ((textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "") || (textBox1.Text != "" && textBox2.Text != "" && textBox4.Text != "") ||
        (textBox1.Text != "" && textBox3.Text != "" && textBox4.Text != "") || (textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
        )
    {                  
        // Code             
    }             
    else             
    {                 
        MessageBox.Show("Fill required fields");             
    }

Upvotes: -1

Youp Bernoulli
Youp Bernoulli

Reputation: 5655

You use OR (||) and should use AND (&&) instead. You want ALL three textboxes to be non-empty strings. Check out the following code:

if (textBox1.Text != String.Empty && textBox2.Text != String.Empty && textBox4.Text != String.Empty)
{
  // Code
}
else
{
  MessageBox.Show("Fill required fields");
}

You can also make a collection of TextBoxes and loop through them to check for non-empty Strings. Something like this:

List<TextBox> _lstTextBoxes = new List<TextBox>();
_lstTextBoxes.Add(textBox1);
_lstTextBoxes.Add(textBox2);
_lstTextBoxes.Add(textBox3);

Boolean checkFailed = false;
foreach(TextBox tb in _lstTextBoxes)
  if(tb.Text == String.Empty)
    checkFailed = true;

if(checkFailed)
  MessageBox.Show("Fill required fields");
else
  //code

This way you have a more generic approach to which you can easily add or remove certain textboxes.

Upvotes: 2

Daren Thomas
Daren Thomas

Reputation: 70324

 if (textBox1.Text != "" &&  textBox2.Text != "" && textBox4.Text != "")
 {
     // Code
 }
 else
 {
     MessageBox.Show("Fill required fields");
 }

You want all conditions to pass. This fits the semantics of the logical AND operator &&.

If you have tons of textboxes, I would tend to keep them in a list:

var boxes = new List<TextBox>{
     textBox1,
     textBox2,
     textBox3,
     //...
};
if (boxes.Any(tb => string.IsNullOrEmpty(tb.Text)))
{
     MessageBox.Show("Fill required fields");
}
else
{
    // Code
}

I also tend to prefer to keep the exception in the if part, returning or throwing an error and ommit an else part, since that is just normal code flow. This keeps the code you expect to run as far to the left as possible.

Upvotes: 15

KV Prajapati
KV Prajapati

Reputation: 94645

You may define a method to test empty strings.

public class Test
{   
  public static bool IsEmpty(params string []args)
    {
        if (args.Length == 0) return true ;
        return args.Any(p => string.IsNullOrEmpty(p));
    }
}

To test strings,

if(!Test.IsEmpty(TextBox1.Text,TextBox2.Text,TextBox3.Text))
 {
   //valid
 }

Upvotes: 3

nfechner
nfechner

Reputation: 17525

You created a collection of or statements, so only one needs to be true in order to continue. Instead you need to and them:

if (textBox1.Text != "" && textBox2.Text != "" && textBox4.Text != "")

Upvotes: 3

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

You should change || to &&

Upvotes: 5

Related Questions