473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing "true" and "false" with Enum?

As a newbie to C#, I am not sure what I can do about this. I would like to
do something like an Enumeration to use "constants" like Yes to indicate
true and No for false. But since there seems to be no underlying 0 or non-
zero for boolean values in C#, I am not sure how to handle this. Any
advice would be appreciated.

Thanks,

JB
Mar 24 '07 #1
9 11169
>As a newbie to C#, I am not sure what I can do about this. I would like to
do something like an Enumeration to use "constants" like Yes to indicate
true and No for false.
So you want a type of your own that can be assigned to a bool? Try
something like this

struct YesOrNo
{
private bool _value;

public YesOrNo(bool b) { _value = b; }

public static implicit operator bool(YesOrNo yon)
{
return yon._value;
}

public static implicit operator YesOrNo(bool b)
{
return new YesOrNo(b);
}

public static YesOrNo Yes { get { return true; } }

public static YesOrNo No { get { return false; } }
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 24 '07 #2
On Sat, 24 Mar 2007 00:49:41 -0700, Jamey Bon <jb***@ue.comwr ote:
[...] I would like to
do something like an Enumeration to use "constants" like Yes to indicate
true and No for false. But since there seems to be no underlying 0 or
non-zero for boolean values in C#, I am not sure how to handle this. Any
advice would be appreciated.
Have you tried simply using "bool" as the underlying type for the enum?
You should be able to get "Yes" and "No" defined just fine that way. For
example:

enum YesNo : bool { No = false, Yes = true };

Or is there something more complicated you're trying to accomplish? If
so, you should clarify that.

Pete
Mar 24 '07 #3
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in
news:op******** *******@petes-computer.local:
On Sat, 24 Mar 2007 00:49:41 -0700, Jamey Bon <jb***@ue.comwr ote:
>[...] I would like to
do something like an Enumeration to use "constants" like Yes to
indica
te
>true and No for false. But since there seems to be no underlying 0
or
>non-zero for boolean values in C#, I am not sure how to handle this.
A
ny
>advice would be appreciated.

Have you tried simply using "bool" as the underlying type for the
enum? You should be able to get "Yes" and "No" defined just fine
that way. For example:

enum YesNo : bool { No = false, Yes = true };

Or is there something more complicated you're trying to accomplish?
If so, you should clarify that.

Pete
Hmmm. I believe I failed to think this out properly before I asked the
question. What I really wanted was the ability to directly assign "Yes"
instead of "true" to a boolean variable, like this:

bool isComplete;
isComplete = Yes;

Something like this is possible in some other languages by several means
that all boil down to making false, by whatever name (here it would be
"No") = 0, and true (or, "Yes") = 1. But since C# doesn't treat boolean
values as 0 and 1, I am not sure this is possible.

What you describe should allow me to (if I have the C# syntax correct) do
something like this:

bool isComplete;
isComplete = YesNo.Yes;

This will actually work for my immediate needs and serves to make me
think more clearly about how boolean values are treated in C#. Thank you
for your help.

Now let me go digest what Mattias has been kind enough to post for me.

JB

JB

Mar 24 '07 #4
Jamey Bon <jb***@ue.comwr ote:
Hmmm. I believe I failed to think this out properly before I asked the
question. What I really wanted was the ability to directly assign "Yes"
instead of "true" to a boolean variable, like this:

bool isComplete;
isComplete = Yes;
Can I ask *why* you want to do this, rather than using true/false like
everyone else does? Bear in mind that anyone else reading your code
will expect true/false rather than Yes/No.

--
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
Mar 24 '07 #5
To me, that speaks volumes. I wish I had a dollar for every time I've seen
people attempt to "customize" a framework that already works perfectly well...
Pete
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jon Skeet [C# MVP]" wrote:
Jamey Bon <jb***@ue.comwr ote:
Hmmm. I believe I failed to think this out properly before I asked the
question. What I really wanted was the ability to directly assign "Yes"
instead of "true" to a boolean variable, like this:

bool isComplete;
isComplete = Yes;

Can I ask *why* you want to do this, rather than using true/false like
everyone else does? Bear in mind that anyone else reading your code
will expect true/false rather than Yes/No.

--
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
Mar 24 '07 #6
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Can I ask *why* you want to do this, rather than using true/false like
everyone else does? Bear in mind that anyone else reading your code
will expect true/false rather than Yes/No.
I couldn't agree more! What an utter waste of time and effort...
Mar 24 '07 #7
Jamey Bon wrote:
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in
news:op******** *******@petes-computer.local:
>On Sat, 24 Mar 2007 00:49:41 -0700, Jamey Bon <jb***@ue.comwr ote:
>>[...] I would like to
do something like an Enumeration to use "constants" like Yes to
indica
te
>>true and No for false. But since there seems to be no underlying 0
or
>>non-zero for boolean values in C#, I am not sure how to handle this.
A
ny
>>advice would be appreciated.
Have you tried simply using "bool" as the underlying type for the
enum? You should be able to get "Yes" and "No" defined just fine
that way. For example:

enum YesNo : bool { No = false, Yes = true };

Or is there something more complicated you're trying to accomplish?
If so, you should clarify that.

Pete

Hmmm. I believe I failed to think this out properly before I asked the
question. What I really wanted was the ability to directly assign "Yes"
instead of "true" to a boolean variable, like this:

bool isComplete;
isComplete = Yes;

Something like this is possible in some other languages by several means
that all boil down to making false, by whatever name (here it would be
"No") = 0, and true (or, "Yes") = 1. But since C# doesn't treat boolean
values as 0 and 1, I am not sure this is possible.

What you describe should allow me to (if I have the C# syntax correct) do
something like this:

bool isComplete;
isComplete = YesNo.Yes;

This will actually work for my immediate needs and serves to make me
think more clearly about how boolean values are treated in C#. Thank you
for your help.

Now let me go digest what Mattias has been kind enough to post for me.
You can use a structure and implicit conversion to bool, something like
this:

public struct Affirmation {

private bool _positive;

private Affirmation(boo l positive) {
_positive = positive;
}

public static Affirmation Yes {
get { return new Affirmation(tru e); }
}

public static Affirmation No {
get { return new Affirmation(fal se); }
}

public static implicit operator bool(Affirmatio n a) {
return a._positive;
}

}

bool isComplete;
isComplete = Affirmation.Yes ;

This will call the Yes factory method that creates an Affirmation value
containing a true value, then the implicit conversion operator will
convert the Affirmation value to a bool value.

This structure only allows the Yes and No factory methods to create an
Affirmation value. If you also want to be able to create a value from a
boolean, you can make the constructor public, or add an implicit
conversion from bool to Affirmation.

The enum solution is clearly simpler, but a structure give you more
control over how it's used.

--
Göran Andersson
_____
http://www.guffa.com
Mar 24 '07 #8
On Sat, 24 Mar 2007 03:00:57 -0700, Jamey Bon <jb***@ue.comwr ote:
Hmmm. I believe I failed to think this out properly before I asked the
question. What I really wanted was the ability to directly assign "Yes"
instead of "true" to a boolean variable, like this:

bool isComplete;
isComplete = Yes;
I see. Well, it appears to me you've already gotten a couple of examples
of code that may work fine for you. Göran's looks closer to what you're
asking for, but both are more on the right track than the enum, given your
clarification.

However, I'd have to agree that it would probably make more sense to just
use variable names that correspond better to just using a bool, if you're
just going to treat the variable as a bool anyway. In fact, in your
example, I fail to see how "Yes" is in any way better than "true". A
variable with an "is" at the front is classic boolean everywhere else in
..NET, and I see no benefit to using "yes" or "no" as an assignment rather
than the original boolean values.

For what it's worth:
What you describe should allow me to (if I have the C# syntax correct) do
something like this:

bool isComplete;
isComplete = YesNo.Yes;
Actually, the enum doesn't let you do this. You need to cast the enum
explicitly for it to work. Between the explicit cast and having to
reference the enum type, just using the original "bool" values sure looks
more and more desirable to me. :) You could bypass the enum by declaring
a couple of constants within your class that match the enum values, but
that's even more kludgy and you still need to explicitly cast even then.

Pete
Mar 24 '07 #9
On 24 Mar 2007 07:49:41 GMT, Jamey Bon wrote:
As a newbie to C#, I am not sure what I can do about this. I would like to
do something like an Enumeration to use "constants" like Yes to indicate
true and No for false. But since there seems to be no underlying 0 or non-
zero for boolean values in C#, I am not sure how to handle this. Any
advice would be appreciated.

Thanks,

JB
Just out of curiosity -- what's wrong with perfectly functional constants
like true and false? Keep in mind if you do develop some other way to
repesent true and false, you must also re-implement the necessary boolean
logic, such as not true is false and not false is true etc.

Is it really worth the extra effort? Perhaps you could explain what you're
trying to do
--
Bits.Bytes
http://bytes.thinkersroom.com
Mar 25 '07 #10

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

Similar topics

9
4772
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the variable $noFile, it is print "1" instead of "true". Why does it do this? The loop is failing to run a second time, for reasons I can not fathom. debugNotes tells me that the count of $dirArray is 10, and the file hasn't been found yet, so I...
10
5241
by: Daniel Crespo | last post by:
Hello to all, How can I do new_variable = (variable) ? True : False; in Python in one line? I want to do something like this:
59
4576
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
1
9189
by: eclipse93081 | last post by:
I need a way to sum check boxes in Access. On the database interface I have 3 boxes you can check; "Scheduled/Went On", "Scheduled/Did Not Go On", and "Not Scheduled". I have the boxes set up as "True/False" in design view so on my report it will assign either a 1 if true, or a 0 if false. I need a way to sum each category by itself so I can keep track of how many times each box is checked every month so that when I print out a report it...
0
8306
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
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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
8605
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
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.