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.
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:
I also tend to prefer to keep the exception in the
ifpart, returning or throwing an error and ommit anelsepart, since that is just normal code flow. This keeps the code you expect to run as far to the left as possible.