Changing "true" and "false" with Enum?

  • Thread starter Thread starter Jamey Bon
  • Start date Start date
J

Jamey Bon

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
 
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
 
[...] 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
 
[...] 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
 
Jamey Bon said:
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.
 
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...
 
Jamey said:
[...] 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.
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top