tags:

views:

409

answers:

10

I have two text boxes and I want skip a block of code only when both are empty:

if (txtBox1.Text.Trim() != string.Empty && txtBox2.Text.Trim() != string.Empty )
{
     // Do something
}

If either of the text boxes has something, I want the //Do something part to execute. Only when both are empty do I want to skip.

However the above code fragment doesn't work. Why?

+11  A: 

You should replace your && with ||. Currently, the code in the if block will only be executed if both text fields have text in them.

Thomas Owens
I don't know who downvoted you :( But thanks for the info.
John Klasius
It should have been an upvote, but I got called away from my desk for a few minutes and now I can't change it. :-(
Sukotto
@Sukotto: Just click the up arrow and it should change your vote. Is it not working?
Lucas Jones
I believe there is a time limit, I will +1 for Sukotto to make up for it.
Anthony Forloney
+24  A: 

I believe you have your logical operators mixed up. What you're looking for is

if (txtBox1.Text.Trim() != string.Empty || txtBox2.Text.Trim() != string.Empty )
{
     // Do something
}
CrimsonX
Oh thanks, i'm new to C# and didn't see the obvious. Thanks!
John Klasius
It's more a general logic error than a C# one. For interest read up about [Contrapositives][1]. [1]: http://en.wikipedia.org/wiki/Contraposition
Marius
+3  A: 

You want || instead of &&.

Jimmy
+1  A: 

if (txtBox1.Text.Trim() != string.Empty || txtBox2.Text.Trim() != string.Empty ) { // Do something }

You are using && which will require BOTH of them do be NOT empty. You should use || (OR) to get the desired result.

Flo
A: 

Might be easier to understand:

if (!(txtBox1.Text.Trim() == string.Empty && txtBox2.Text.Trim() == string.Empty ))
{
     // Do something
}
palindrom
+6  A: 

Those who pointed out you need || instead of && are right. If you prefer &&, you could also use:

if (!(txtBox1.Text.Trim() == string.Empty && txtBox2.Text.Trim() == string.Empty))
{
     // Do something
}

The difference is purely aesthetic, but amounts to checking (in English): "It isn't true that both textboxes are empty" as opposed to "It's true that at least one textbox isn't empty." Same meaning, different way of putting it.

For the record, the way you had it in your original question was: "It's true that both textboxes are not empty."

Dan Tao
+2  A: 

I've split the BothBoxesEmpty out to a separate variable to make it more readable... all the brackets become an unreadable mess otherwise. You can merge the two statements if you choose:

bool BothBoxesEmpty = string.IsNullOrEmpty(TextBox1.Text.Trim()) &&
                      string.IsNullOrEmpty(TextBox2.Text.Trim());
if (!BothBoxesEmpty)
{
    /* Do your stuff */
}

You could equally easily replace the string.IsNullOrEmpty with TextBox1.Text.Trim() == string.Empty as TextBox1.Text will always return a string (empty or not) and will never return null.

BenAlabaster
A: 

If you're in a method you could do something like this as well:

 if (txtBox1.Text.Trim() == string.Empty && txtBox2.Text.Trim() == string.Empty )
      return;
 //Do Something
jamesaharvey
A: 

You could make it more concise by concatenating the strings, then testing:

if ((txtBox1.Text.Trim() + txtBox2.Text.Trim()) != string.Empty )
{
    // Do something
}

Depending on what the boxes represent this could be somewhat less obvious, though.

NVRAM
A: 

This reminds me of a story I heard about a junior coder who was told to implement rules for calculating insurance benefits, where "everyone under 18 and over 65" did not qualify. So he coded it as:

if (age < 18 and age > 65) ...

The story goes that his coworkers tried repeatedly to convince him that his logic would not work correctly, but he stubbornly refused to understand or change it.

Loadmaster
I guess that’s product of the direct translation of the english statement… :)
Martín Marconcini