views:

205

answers:

9

Is it preferable to explicitly set enum's fields instead of just defining their names? E.g. any pros and cons for Enum1 vs Enum2?

Enum1:

enum SomeEnum
{
   Something1 = 0,
   Something2 = 1
}

Enum2:

enum SomeEnum
{
   Something1,
   Something2
}

P.S. This question doesn't relates to enums that would be stored in database, which is requires to set values explicitly.

Edit: Say all values are 0, 1, etc... They are not negative or something.

+4  A: 

You would use Enum1 if you need values that are not contiguous.

enum Colors 
{ 
    Red = 1, 
    Green = 2, 
    Blue = 4, 
    Yellow = 8 
};

Also, explicitly setting the numbers is easier to read, and adding a value in the middle doesn't change any of the other values.

Robert Harvey
Hi Robert,What do you mean by "adding a value in the middle doesn't change any of the other values"
Jim
If he added Purple = 3 in the middle, it wouldn't change the values of Blue or Yellow.
Donblas
True, but adding it to the end wouldn't affect the values either!?
Jim
There are reasons why you may want your enums in a particular order. I always specify the values.
Robert Harvey
Hi Robert,Im not saying you're wrong! The OP asked what are the reasons for either method. Other than wanting to know that Red is equal to 1, ehat other reasons?
Jim
To alphabetize them, maybe. To put them in a logical order.
Robert Harvey
Hmmm, I unserstand what you are saying - however as they appear alphabetically through intellisense when coding I still can't see a reason.
Jim
Yeah, I'm with Jim on this; order isn't really all that important.
Randolpho
+4  A: 

it's useful when you need to have negative values

enum
{
  Error = -1,
  Success = 0,
  Something1, 
  Something2,
};
Max
Negative values add nothing to an enumeration.
Randolpho
@Randolpho: enum.s are often used in wrapping APIs for other apps. Eg: C#/.NET wrapper around the Google Data API. It adds a great deal if you can enumerate all return values including negatives that the originator decided to use.
Dinah
@Dinah: I'm aware of that (see my answer on this page). And for wrapping old C APIs, yes, negative values for an enumeration are frequently required. But I stand by my statement: negative values in an enumeration provide no real value. The same was true back in the days of C. Still is.
Randolpho
+1  A: 

I've run into problems serializing enums that don't contain the value 0, when using them over a web services interface.

Phillip Ngan
Just use `[EnumMember(Value = "EnumerantValue")]`
Randolpho
If I'm not wrong, this don't work in VB.NET
Kamarey
Not as written, but it does work like this: `<EnumMember(Value:="EnumerantValue")>`
Randolpho
+2  A: 

Use example 1 if you actually need to have numbers (i.e. you need to know that something2 actually equals 2)

For all other options do not use numbers. that way, you could happily add additional items to your enum without breaking any other code with may refer to your enum.

Jim
come on then... this answer is not useful apparently? but why?
Jim
The downvote probably occurred because your statement, "without breaking any code," is not 100% true. If an enum value is used in a calculation, and that enum value changes, the code will break.
Robert Harvey
@Robert: well, he did say "add" and not "change"... I don't see a reason for the -1 and compensated.
RedGlyph
Add == change if you add a value in the middle and the values aren't explicitly stated.
Robert Harvey
+5  A: 

Enum1 and Enum2 compile to the same enumeration. So from a programming standpoint, there is no difference. From a maintenance standpoint, there may or may not be issues.

On the one hand, declaring explicit values is absolutely required for a [Flags] enum; and they need to be values at powers of 2.

On the other hand, explicit values can reduce maintainability simply by creating values that cannot or should not be changed in the future.

In general, I prefer to not declare explicit values unless the values themselves are significant for some reason -- if your C# enumeration is, for example, wrapping a C enumeration.

Randolpho
I asked this question mostly by usability and maintainability reasons, so this answer is closest one. Thanks.
Kamarey
+1  A: 

it depends on what you are doing with you enums. If you are using them with [Flags] then you MUST set them explicitly. if you are not, then either way works from a coding standpoint. however, for readability or (as mentioned) use in a database situation, then you should explicitly set them. Another consideration is the coding standards of your workplace, and, serialization as well.

Muad'Dib
+1  A: 

Enum 1 is also necessary when you need to use flagged enums: http://weblogs.asp.net/wim/archive/2004/04/07/109095.aspx

KAM
+2  A: 

Its useful to explicitly set the values if the have some meaning outside of your program. For example, an application might write a log file that uses a Severity value that you can use for filtering the log file later. Setting the values explicitly in an Enum means you can use the Enum in your code (e.g. Critical, Error, Warning) instead of the numeric values. In the log file you would want the numeric value written for easier sorting.

I've also seen an enum used to represent values in a database reference table. In that case, you want to make sure that the items in the enum always have the same value as the database table and that inserting a new item in the middle of the enum doesn't mess up the values.

TLiebe
+1  A: 

If all you want is a set of int-like constants whose values aren't important, use the default. If the values matter then set them.

An example of values that matter is creating values for bit flags that can be set something like this:

> enum 
{
    flag1 = 1;
    flag2 = 2;
    flag3 = 4;
};

func1(flag1 | flag3);
Dr. Tim
You left off the `[Flags]` attribute. Very necessary.
Randolpho