In a C# command-line app I'm writing, several of the parameters have "yes" and "no" as the possible values.
I am storing their input using the Enum type shown below.
enum YesNo
{
Yes,
No
}
Which is fine - the code works. No problem there.
NOTE: Yes, I could store these as bool (that's how it used to work). My design choice is to be explicit about the Yes/No choice made by the user because they will see this printed in other contexts and I'd like it to be more obvious what the choice was.
I asked this question relatively early in StackOverflow's life. It wasn't fake question - I really did have this situation. I just thought it would be nice to use it see what the community would do. Because it is, I admit a somewhat odd question.
First, thanks to all who spent the time replying. I'm trying to pay that back with a thoughtful conclusion.
Comments on the answers
switching to bool. I understand your motivation, but I feel I need to point out that having a binary choice (and by that I mean a choice between any two values - alive/dead, married/unmarried, etc.) is not the same as boolean choice between true and false. We find as programmers switching between yes/no and true/false easy - fair enough. Had my choice in this case been for example "Democrat" or "Replication"" (contrived example, I know) then you can see possibilities for confusion or at least awkwardness. I do think the bool option is valid in this case, but less so in other binary choices.
localization - great point. In my specific case it didn't matter - this was not and is never going to be localized, but for other situations it is something to consider.
more than three options - In fact, later on I had to add a third value called to represent the valid (in my application) condition of a user specifically not making the choice.
There were a lot of good comments, thank you all!
Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy.
The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration is "flag" and the constant // are the values of the flag.
I'd suggest you use a name that indicates the Value which is set to Yes or No.
E.G.
public enum Married
{
YES,
NO
}
You say you don't want to use bool because it will be printed out for the user to see amongst other contents. That suggests the problem isn't in storage but in display. By all means present true/false as Yes/No, but there's no need to create a whole new type for it IMO.
EDIT: In addition to suggesting you don't use an enum in the first place, I'd strongly recommend that if you do use an enum, you change the order or use explicit values. Having Yes=0, No=1 will be really confusing if you ever end up seeing the values as integers.
Or just use a boolean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With