By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,330 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,330 IT Pros & Developers. It's quick & easy.

Changing "true" and "false" with Enum?

P: n/a
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
Share this Question
Share on Google+
9 Replies

P: n/a
>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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.