473,786 Members | 2,615 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
Jul 21 '05 #1
4 1514
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
Jul 21 '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/
Jul 21 '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.
Jul 21 '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
Jul 21 '05 #5

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

Similar topics

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
31622
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...
4
1608
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
16
82850
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
11371
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
11205
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?
0
9647
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
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7512
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
6745
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
5397
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...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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.