If ... Else, Operator problem

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

I have following code lines:

===============================
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear();
txtMethod.Focus();
}
else
{
grpboxDirect.Visible = true;
txtCalculatePercent.Focus();
}
==============================

txtMethod is a TextBox that need to have values only D or F. The first
line is giving problem. Please correct.
 
I have following code lines:

===============================
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear();
txtMethod.Focus();
}
else
{
grpboxDirect.Visible = true;
txtCalculatePercent.Focus();
}
==============================

txtMethod is a TextBox that need to have values only D or F. The first
line is giving problem. Please correct.

Well, you didn't specify what problem you're having, but the code
above won't compile - you're missing an open parenthesis. Also, your
logic doesn't match what you say you want. You likely want:

// "it's not a D, and it's not an F"
if ((txtMethod.Text != "D") && (txtMethod.Text != "F"))

You should consider refactoring to remove the negation; it tends to
make code harder to read.

if ((txtMethod.Text == "D") || (txtMethod.Text == "F"))
{
grpboxDirect.Visible = true;
txtCalculatePercent.Focus();
}
else
{
txtMethod.Clear();
txtMethod.Focus();
}

Michael
 
I corrected the parenthesis problem. Still I have similar code blocks
where ELSE is not needed.
So, something like:

if ((txtMethod.Text == "D") || (txtMethod.Text == "F"))

will not work. I have written this on TextChange event. If the text
type is not D or not F then clear it, else do the ELSE part.
 
I have following code lines:

===============================
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear();
txtMethod.Focus();
}
else
{
grpboxDirect.Visible = true;
txtCalculatePercent.Focus();
}
==============================

txtMethod is a TextBox that need to have values only D or F. The first
line is giving problem. Please correct.

As another posted indicated, you should not combine NOTs with ORs.
You'll get in trouble every time.
 
RP said:
I corrected the parenthesis problem. Still I have similar code blocks
where ELSE is not needed.
So, something like:

if ((txtMethod.Text == "D") || (txtMethod.Text == "F"))

will not work. I have written this on TextChange event. If the text
type is not D or not F then clear it, else do the ELSE part.

I don't see why that wouldn't work.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
As another posted indicated, you should not combine NOTs with ORs.
You'll get in trouble every time.

Not necessarily. In this particular case the two "nots" are exclusive
(the text will always either be "not D" or "not F") but that's not
always the case.

Counter-example:

if (!user.IsAuthenticated || !user.IsAuthorized)
{
// Display login page
}

that's effectively:

if (!(user.IsAuthenticated && user.IsAuthorized))

It makes perfect sense, and there's no "getting in trouble".
 
I corrected the parenthesis problem. Still I have similar code blocks
where ELSE is not needed.
So, something like:

if ((txtMethod.Text == "D") || (txtMethod.Text == "F"))

will not work. I have written this on TextChange event. If the text
type is not D or not F then clear it, else do the ELSE part.

No. The condition you've stated ("if the text is not D or not F")
will not work. Think about it; say you've got:
if ((txtMethod.Text != "D") || (txtMethod.Text != "F"))
You want that to resolve to false when txtMethod is "D", for
instance. What you'll actually see is:
if (( "D != "D") || ("D" != "F"))
which resolves to:
if ( false || true)
which resolves to true.

See my earlier reply for what you likely want to do. In general,
though, it's worthwhile to "run" your algorithms in your mind with
test cases, to see if they do the right thing. Also, while English
often treats "and" and "or" as equivalent, boolean algebra definitely
does not.

Michael
 
Not necessarily. In this particular case the two "nots" are exclusive
(the text will always either be "not D" or "not F") but that's not
always the case.

Counter-example:

if (!user.IsAuthenticated || !user.IsAuthorized)
{
// Display login page

}

that's effectively:

if (!(user.IsAuthenticated && user.IsAuthorized))

It makes perfect sense, and there's no "getting in trouble".

Let me re-phrase. When you try to mix NOTs with ORs, you better know
what you are doing. :-)
 
Let me re-phrase. When you try to mix NOTs with ORs, you better know
what you are doing. :-)

You need to be careful - that's always the case, of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top