473,473 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Are enum values automatically assigned?

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.

--
Sincerely,

David Sworder
http://www.CodeFanatic.com
Nov 16 '05 #1
4 1598
David Sworder wrote:
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.

--
mikeb
Nov 16 '05 #2
David Sworder wrote:
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.

--
Konrad -
http://madrat.net/
Nov 16 '05 #3
> 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.
Nov 16 '05 #4
Hi David,

"David Sworder" <ds******@cts.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Sworder | last post by:
With a standard enum, values are automatically assigned. public enum MyEnum{Happy,Sad,Angry,Grandpa}; ....but what about a bitflag enum? public enum MyEnum:int{ FalseTeeth=0x00000001...
12
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
5
by: DJTB | last post by:
Dear Group, I'd like to check if a value is defined in an enum. Example: ------------------------------------------------------ typedef enum { A_VALUE = 1,
36
by: codymanix | last post by:
why do I always have to write the name of the enum type in front of the enum member? why not simply writing Red instead of Color.Red? The compiler always knows which type is expected so there...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
9
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
8
by: Mark P | last post by:
I'm working on a project where I have something like enum Modes {mode_a, mode_b, ...}; Due to project spec changes, the number of Modes has increased several times. There are a few places...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.