473,396 Members | 1,789 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,396 software developers and data experts.

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 11124
>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.comwrote:
[...] 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*********@nnowslpianmk.comwrote in
news:op***************@petes-computer.local:
On Sat, 24 Mar 2007 00:49:41 -0700, Jamey Bon <jb***@ue.comwrote:
>[...] 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.comwrote:
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.com>
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.comwrote:
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.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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*********@nnowslpianmk.comwrote in
news:op***************@petes-computer.local:
>On Sat, 24 Mar 2007 00:49:41 -0700, Jamey Bon <jb***@ue.comwrote:
>>[...] 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(bool positive) {
_positive = positive;
}

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

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

public static implicit operator bool(Affirmation 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.comwrote:
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
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...
10
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
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 ...
1
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...
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
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
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...
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
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...
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.