473,799 Members | 3,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are enum values automatically assigned?

With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sa d,Angry,Grandpa };

....but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00 000001
BrainCancer=0x0 0000002
CronesDisease=0 x00000004
AllDiseasesAndP roblems=0xfffff fff
}

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 1608
David Sworder wrote:
With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sa d,Angry,Grandpa };

...but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00 000001
BrainCancer=0x0 0000002
CronesDisease=0 x00000004
AllDiseasesAndP roblems=0xfffff fff
}

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,Sa d,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=0x00 000001
BrainCancer=0x0 0000002
CronesDisease=0 x00000004
AllDiseasesAndP roblems=0xfffff fff
}


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.c om> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl...
With a standard enum, values are automatically assigned.

public enum MyEnum{Happy,Sa d,Angry,Grandpa };

...but what about a bitflag enum?

[Flags()]
public enum MyEnum:int{
FalseTeeth=0x00 000001
BrainCancer=0x0 0000002
CronesDisease=0 x00000004
AllDiseasesAndP roblems=0xfffff fff
}

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,

AllDiseasesAndP roblems = FalseTeeth|Brai nCancer|CronesD isease
}

Regards,
Daniel
Nov 16 '05 #5

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

Similar topics

4
1515
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 BrainCancer=0x00000002
12
3428
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 static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
5
31626
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
1735
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 cannot be a collision and the compiler can simply look up the enum member in the expected type. wouldn't that be easier and simpler? I see not technical reason for that verbose syntax. In c++, writing the type is also not neccesary, so why is it...
16
82855
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
3073
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: to catch unhandled cases, but of course this is only at run-time. Example: public enum MyEnum { one, two, three, four }
18
11372
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
53806
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
11206
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
8
32821
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 where it's useful to know the number of image modes (to set the size of bitsets, etc.) Currently I just use a const int to hold this size: enum Modes {mode_a, mode_b, mode_c};
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.