Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

If ... Else, Operator problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RP - 29 Aug 2007 18:32 GMT
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.
Chris Shepherd - 29 Aug 2007 18:42 GMT
[...]
You are missing an opening bracket:
> if (txtMethod.Text != "D") || (txtMethod.Text != "F"))

should be:
> if ((txtMethod.Text != "D") || (txtMethod.Text != "F"))

Chris.
mpetrotta@gmail.com - 29 Aug 2007 18:46 GMT
> I have following code lines:
>
[quoted text clipped - 13 lines]
> 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
RP - 29 Aug 2007 18:52 GMT
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.

> 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
[quoted text clipped - 19 lines]
>
> Michael
Jon Skeet [C# MVP] - 29 Aug 2007 19:05 GMT
> I corrected the parenthesis problem. Still I have similar code blocks
> where ELSE is not needed.
[quoted text clipped - 4 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

mpetrotta@gmail.com - 29 Aug 2007 19:13 GMT
> I corrected the parenthesis problem. Still I have similar code blocks
> where ELSE is not needed.
[quoted text clipped - 4 lines]
> 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
zacks@construction-imaging.com - 29 Aug 2007 18:58 GMT
> I have following code lines:
>
[quoted text clipped - 13 lines]
> 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.
Jon Skeet [C# MVP] - 29 Aug 2007 19:11 GMT
> 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".

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

zacks@construction-imaging.com - 29 Aug 2007 19:52 GMT
>  <za...@construction-imaging.com> wrote:
> > As another posted indicated, you should not combine NOTs with ORs.
[quoted text clipped - 17 lines]
>
> 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. :-)
Jon Skeet [C# MVP] - 29 Aug 2007 20:00 GMT
> > 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. :-)

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.