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

Home Posts Topics Members FAQ

Enumeration values

This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.

What I would like to do is create an enum where the varaibles increment by
some formula rather than just 1. I doubt this is possible but it would be
nice

Essentially what I'm having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

This is be I'm trying to "oop" some flags that are packed in a specific
way(not under my control but done my microsoft... see
http://msdn2.microsoft.com/en-us/library/ms902086.aspx)

It would just be nicer if I could somehow automatically have the compiler do
the math so

enum E
{
A = StartValue,
B,
C,
D
}

and it give the right values.

I'm sure theres no way to do this but I thought I would ask(I guess I could
write a pre-processor but thats to much work.

Thanks,
Jon
Sep 24 '07 #1
11 1706
Jon Slaughter <Jo***********@Hotmail.comwrote:
This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.
No, I don't believe so. To be honest, it would be used sufficiently
rarely that I don't think it would justify the extra complexity in the
language.

The one exception to that is Flags enums. I wish when I declared an
enum of

[Flags]
public enum Values
{
Foo,
Bar,
Baz
}

it would use 1, 2 and 4 automatically. Unfortunately you've got to do
it manually - and this is a very widely used pattern.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '07 #2
Hi it will work
Essentially what I'm having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

what u want more

"Jon Slaughter" wrote:
This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.

What I would like to do is create an enum where the varaibles increment by
some formula rather than just 1. I doubt this is possible but it would be
nice

Essentially what I'm having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

This is be I'm trying to "oop" some flags that are packed in a specific
way(not under my control but done my microsoft... see
http://msdn2.microsoft.com/en-us/library/ms902086.aspx)

It would just be nicer if I could somehow automatically have the compiler do
the math so

enum E
{
A = StartValue,
B,
C,
D
}

and it give the right values.

I'm sure theres no way to do this but I thought I would ask(I guess I could
write a pre-processor but thats to much work.

Thanks,
Jon
Sep 24 '07 #3
On Sep 23, 11:35 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>
No, I don't believe so. To be honest, it would be used sufficiently
rarely that I don't think it would justify the extra complexity in the
language.
Jon you really hate Enums I notice. Just like I hate nested classes.
Especially after that last thread, where I 'learned' nested classes
like pulling teeth. LOL. Learning the hard way.

RL

Sep 24 '07 #4
On Sep 24, 10:27 am, raylopez99 <raylope...@yahoo.comwrote:
No, I don't believe so. To be honest, it would be used sufficiently
rarely that I don't think it would justify the extra complexity in the
language.

Jon you really hate Enums I notice.
Um, what makes you think that? I think they could be better - there
are certainly issues with them in their current form - but they're
still very useful.
I just don't think that you need to apply a formula to determine the
value very often.

So, could you point to some evidence that I "really hate" enums?

For reference, here's an article about the kind of improvement I'd
like to see:
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx
Just like I hate nested classes.
Especially after that last thread, where I 'learned' nested classes
like pulling teeth. LOL. Learning the hard way.
You started from an incorrect assumption. There's no reason to hate
nested classes.

Jon

Sep 24 '07 #5
The one exception to that is Flags enums. I wish when I declared an
enum of

[Flags]
public enum Values
{
Foo,
Bar,
Baz
}

it would use 1, 2 and 4 automatically. Unfortunately you've got to do
it manually - and this is a very widely used pattern.
There are times, however, when you don't want the flags to increment by
doubling, such as when you want to include values that combine other values
(such as a value of 3 for Foo|Bar). I'm presuming that you mean by default?

--

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Jon Slaughter <Jo***********@Hotmail.comwrote:
>This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields
is
incremented by something rather than 1.

No, I don't believe so. To be honest, it would be used sufficiently
rarely that I don't think it would justify the extra complexity in the
language.

The one exception to that is Flags enums. I wish when I declared an
enum of

[Flags]
public enum Values
{
Foo,
Bar,
Baz
}

it would use 1, 2 and 4 automatically. Unfortunately you've got to do
it manually - and this is a very widely used pattern.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 24 '07 #6
On Sep 24, 12:27 pm, "Kevin Spencer" <unclechut...@nothinks.com>
wrote:
it would use 1, 2 and 4 automatically. Unfortunately you've got to do
it manually - and this is a very widely used pattern.

There are times, however, when you don't want the flags to increment by
doubling, such as when you want to include values that combine other values
(such as a value of 3 for Foo|Bar). I'm presuming that you mean by default?
Whichever way round you wanted it, you'd certainly have to be able to
manually override things. Either making it double values by default,
or being able to explicitly state it, e.g. with
[Flags(AutomaticValues=true)] would be fine. As it is, you pretty much
*always* need to explicitly put the values on.

Jon

Sep 24 '07 #7
Jon Slaughter <Jo***********@Hotmail.comwrote:
If you looked at that link I sent then you would see that this is exactly
what I'm tryign to accomplish(Scroll to the very bottom).
No, it's not exactly what you're trying to accomplish. You're trying to
accomplish a custom set of enum values beyond just doubling. I was
suggesting that it would be worth putting in simple doubling as part of
what the compiler was able to do, but *not* putting full-blown support
for arbitrary functions to be applied.

The method and access parts are simple bitflags, but the other values
aren't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '07 #8

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Jon Slaughter <Jo***********@Hotmail.comwrote:
>If you looked at that link I sent then you would see that this is exactly
what I'm tryign to accomplish(Scroll to the very bottom).

No, it's not exactly what you're trying to accomplish. You're trying to
accomplish a custom set of enum values beyond just doubling. I was
suggesting that it would be worth putting in simple doubling as part of
what the compiler was able to do, but *not* putting full-blown support
for arbitrary functions to be applied.
It is, I'm simply trying to create flags... is not different than
incrementing by 2^k which is what your doing except the formula is slightly
different. In reality yours is more complicated because it involves having
to compute the power's of 2 while mine is a simple linear equation mx + b.
(of course computing 2^k is easier by shifting but both are actually very
simple to do).

No one said anything about full blown support for arbitrary functions.
The method and access parts are simple bitflags, but the other values
aren't.

Look. for the CTL_CODE it is a simple increment but shift 2 bits over + an
additional mask for other flags. Its not as complicated as you think.

It could easily be done by adding a support function that takes the flags
such as

enum someflags
{
f1 = 0,
f2,
f3,
f4
}
and

int convertflags(someflags s)
{

return ((int)s) << 2 + base_Code;
}
while yours would be

int convertflag(someflags s)
{
return (s == 0) ? 0 : 1 << (((int)s) - 1);
}

(in the second case you would have to convert individual flags first then
combine them such as convertflag(f1) | convertflag(f2) while I'm not really
using flags but identifiers. (Although I could use flags if I wanted they
don't really decompose into flags easily).
All they would have to do is extend the syntax and grammar to support a
helper function such as I have did

enum someflags : convertflags
{
f1 = 0,
f2,
f3,
f4
}

and essentially the preprocessor will wrap every use of one of the variables
with the function. (not saying it should be done just like that but I'm
saying its not difficult at all and could be done with a preprocessor
completely).

Sep 24 '07 #9

Don't get me wrong though. I'm not saying it should be implement because it
might not be in the "style" of C#. But if some method could be used in a
natural way then maybe it should even if its rarely used. I suppose one
could just write a class instead of the enum and do it that way but then it
sorta looses the "enum" type of behavior I think. If an enum could have a
"constructor" or method that wraps every use of an enum variable then it
could work easily(of course then it becomes a run-time behavior instead.
(maybe they could precompile that code and use it to determine the values
durring the pre-processor though so its compile time instead)


Sep 24 '07 #10
(maybe they could precompile that code and use it to determine the values
durring the pre-processor though so its compile time instead)
I guess what the preprocessor/compiler could try and do is compile the code
beforhand and see if it can preprocess the enums and if not then use the
code as a wrapper at run-time(maybe giving a warning about performance).
(this may break many things and not be useful though.. just an idea... not
well thought out so don't bitch if there are some blatent logical issues
with it)
Sep 24 '07 #11
Jon Slaughter <Jo***********@Hotmail.comwrote:
No, it's not exactly what you're trying to accomplish. You're trying to
accomplish a custom set of enum values beyond just doubling. I was
suggesting that it would be worth putting in simple doubling as part of
what the compiler was able to do, but *not* putting full-blown support
for arbitrary functions to be applied.

It is, I'm simply trying to create flags... is not different than
incrementing by 2^k which is what your doing except the formula is slightly
different. In reality yours is more complicated because it involves having
to compute the power's of 2 while mine is a simple linear equation mx + b.
(of course computing 2^k is easier by shifting but both are actually very
simple to do).
It's not a matter of the computation - it's a matter of the expression
of it. A simple "always double" is easy to express as a boolean (do it
or don't). Anything else requires more complexity - a complexity which
I don't think is worth it for the very limited set of use cases.

In other words: a simple solution would cover 95% of cases. Making it
more complex might cover an additional 2 or 3%, and the remainder may
be infeasible. I don't think it's worth introducing more complexity for
that 2-3%, but the 95% case is worth tackling.

<snip>
All they would have to do is extend the syntax and grammar to support a
helper function such as I have did

enum someflags : convertflags
{
f1 = 0,
f2,
f3,
f4
}

and essentially the preprocessor will wrap every use of one of the variables
with the function. (not saying it should be done just like that but I'm
saying its not difficult at all and could be done with a preprocessor
completely).
At that point it's no longer an enum in the normal .NET sense, where
the values are defined as constants.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '07 #12

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

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
1
by: Jamie Winder via .NET 247 | last post by:
Is it possible to iterate through all of the possible values of an enumeration? (with foreach, maybe?) What I need to do is fill a ComboBox with all possible values for an enumeration. e.g ...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
5
by: Dave | last post by:
How would I go about filling a combo box with some existing enumeration values? Can I bind it to the enumeration? Then, once the selection is made from the combo box, set a property to the...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
0
by: puvit82 | last post by:
My problem is as follows, any advice / suggestion would be greatly appreciated: Lets suppose that I have defined a simpleType "addressType" with 3 enumeration values (Home, Office, Vacation)...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
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
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.