473,786 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

If ... Else, Operator problem

RP
I have following code lines:

=============== =============== =
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear ();
txtMethod.Focus ();
}
else
{
grpboxDirect.Vi sible = true;
txtCalculatePer cent.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 1593
RP wrote:
[...]
You are missing an opening bracket:
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
should be:
if ((txtMethod.Tex t != "D") || (txtMethod.Text != "F"))
Chris.
Aug 29 '07 #2
On Aug 29, 9:32 am, RP <rpk.gene...@gm ail.comwrote:
I have following code lines:

=============== =============== =
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear ();
txtMethod.Focus ();
}
else
{
grpboxDirect.Vi sible = true;
txtCalculatePer cent.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.Tex t != "D") && (txtMethod.Text != "F"))

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

if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
{
grpboxDirect.Vi sible = true;
txtCalculatePer cent.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.Tex t == "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.Tex t != "D") && (txtMethod.Text != "F"))

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

if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
{
grpboxDirect.Vi sible = true;
txtCalculatePer cent.Focus();}

else
{
txtMethod.Clear ();
txtMethod.Focus ();

}

Michael

Aug 29 '07 #4
On Aug 29, 1:32 pm, RP <rpk.gene...@gm ail.comwrote:
I have following code lines:

=============== =============== =
if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
{
txtMethod.Clear ();
txtMethod.Focus ();
}
else
{
grpboxDirect.Vi sible = true;
txtCalculatePer cent.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*********@gm ail.comwrote:
I corrected the parenthesis problem. Still I have similar code blocks
where ELSE is not needed.
So, something like:

if ((txtMethod.Tex t == "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.co m>
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***@construc tion-imaging.comwrot e:
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.IsAuthen ticated || !user.IsAuthori zed)
{
// Display login page
}

that's effectively:

if (!(user.IsAuthe nticated && user.IsAuthoriz ed))

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

--
Jon Skeet - <sk***@pobox.co m>
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...@gm ail.comwrote:
I corrected the parenthesis problem. Still I have similar code blocks
where ELSE is not needed.
So, something like:

if ((txtMethod.Tex t == "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.Tex t != "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.co mwrote:
<za...@construc tion-imaging.comwrot e:
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.IsAuthen ticated || !user.IsAuthori zed)
{
// Display login page

}

that's effectively:

if (!(user.IsAuthe nticated && user.IsAuthoriz ed))

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***@construc tion-imaging.comwrot e:
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.co m>
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
2441
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 double, I have to define a global operator+ (or -*/) and special function classes. --- source begins here ---
0
1836
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 be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
11
912
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 nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
3
2051
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 fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
4
2483
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 templatized operator>>() as a member that can deal with the built in C types (int, char, float...) template <class tType> STR& STR::operator>>(tType & t); I then attempted to add an overloaded version of this to support my own
24
1827
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
2148
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 if the parameter model_name is "aa", then choose ModelAA. Currently I do this as follows:
22
3625
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 v);
1
10613
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 I get column does not exist for the else clause (all columns starting with shift through sequence) I'm sure this is a simple problem, and I apologize in advance. USE GO /****** Object: StoredProcedure . Script Date: 10/20/2008 11:54:55...
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.