Are enum values automatically assigned?

  • Thread starter Thread starter David Sworder
  • Start date Start date
D

David Sworder

With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sad,Angry,Grandpa};

....but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00000001
BrainCancer=0x00000002
CronesDisease=0x00000004
AllDiseasesAndProblems=0xffffffff
}

In this example, is it necessary for me to manually assign the values to
'FalseTeeth', 'BrainCancer', and 'CronesDisease' or does the compiler do
this for me automatically? For small enums, I don't really mind the
inconvenience of manually typing the associated bitvalues. However, it's
more of a pain for larger enums -- especially when I need to insert an item
in the enum at some later time and all of the bitflag values must be
adjusted.
 
David said:
With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sad,Angry,Grandpa};

...but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00000001
BrainCancer=0x00000002
CronesDisease=0x00000004
AllDiseasesAndProblems=0xffffffff
}

In this example, is it necessary for me to manually assign the values to
'FalseTeeth', 'BrainCancer', and 'CronesDisease' or does the compiler do
this for me automatically? For small enums, I don't really mind the
inconvenience of manually typing the associated bitvalues. However, it's
more of a pain for larger enums -- especially when I need to insert an item
in the enum at some later time and all of the bitflag values must be
adjusted.

The bitflag attribute does not change the way values are assigned to the
various enum members. You'll need to explicitly set the values for your
bitmap enums.
 
David said:
With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sad,Angry,Grandpa};

...but what about a bitflag enum?

test it.
Answer: no, they are enumerated 0, 1, 2, 3 ... which isn't helpful. You
need to do the assignments manually.
[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00000001
BrainCancer=0x00000002
CronesDisease=0x00000004
AllDiseasesAndProblems=0xffffffff
}

The leading '0's are not necessary. Leave them out. That would save you
some work at least.
 
test it.
Answer: no, they are enumerated 0, 1, 2, 3 ...

Yeah, I noticed that.... that's why I was hoping that I was missing
something. I thought maybe I was misusing the Flags() attribute.... ok,
well, thanks for the clarification.
 
Hi David,

David Sworder said:
With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sad,Angry,Grandpa};

...but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00000001
BrainCancer=0x00000002
CronesDisease=0x00000004
AllDiseasesAndProblems=0xffffffff
}

In this example, is it necessary for me to manually assign the values to
'FalseTeeth', 'BrainCancer', and 'CronesDisease' or does the compiler do
this for me automatically? For small enums, I don't really mind the
inconvenience of manually typing the associated bitvalues. However, it's
more of a pain for larger enums -- especially when I need to insert an item
in the enum at some later time and all of the bitflag values must be
adjusted.

For larger enums, you *might* find something like this easier:

[Flags()]
public enum MyEnum
{
FalseTeeth = 1,
BrainCancer = FalseTeeth << 1,
CronesDisease = BrainCancer << 1,

AllDiseasesAndProblems = FalseTeeth|BrainCancer|CronesDisease
}

Regards,
Daniel
 
Back
Top