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 4 1499
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
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/
> 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" <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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,
|
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...
|
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...
|
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
|
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:...
|
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
|
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) { ... }
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |