473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

If ... Else, Operator problem

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.

Aug 29 '07 #1
9 1578
RP wrote:
[...]
You are missing an opening bracket:
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
should be:
if ((txtMethod.Text != "D") || (txtMethod.Text != "F"))
Chris.
Aug 29 '07 #2
On Aug 29, 9:32 am, RP <rpk.gene...@gmail.comwrote:
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

Aug 29 '07 #3
RP
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
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

Aug 29 '07 #4
On Aug 29, 1:32 pm, RP <rpk.gene...@gmail.comwrote:
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.

Aug 29 '07 #5
RP <rp*********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@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
Aug 29 '07 #6
<za***@construction-imaging.comwrote:
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".

--
Jon Skeet - <sk***@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
Aug 29 '07 #7
On Aug 29, 9:52 am, RP <rpk.gene...@gmail.comwrote:
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

Aug 29 '07 #8
On Aug 29, 2:11 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
<za...@construction-imaging.comwrote:
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".
Let me re-phrase. When you try to mix NOTs with ORs, you better know
what you are doing. :-)

Aug 29 '07 #9
<za***@construction-imaging.comwrote:
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.

--
Jon Skeet - <sk***@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
Aug 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
24
by: gupta.keshav | last post by:
HI, Is there any situation which can be handled by ternary operator but not with if else blocks? Thanks Keshav
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
1
by: Jeffy | last post by:
I'm trying to create an if/else case in a stored procedure where if te record is not found, it returns blank values, and if it is found I get the real values. But when I try to execute the SP update...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.