Enumeration values

J

Jon Slaughter

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
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
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.
 
G

Guest

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
 
R

raylopez99

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
 
J

Jon Skeet [C# MVP]

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/archive/2006/01/05/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
 
K

Kevin Spencer

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 said:
Jon Slaughter said:
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.
 
J

Jon Skeet [C# MVP]

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
 
J

Jon Slaughter

Jon Skeet said:
Jon Slaughter said:
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.


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).

Its just that the contol code is a concatenation of flags. But for my
purposes all others are constant except the function.. which happens to be
the bits 2 - 14. If microsoft would have swaps the order and put the
function first(which seems more logical since that is the one that most
often is changing) then simple enums would work fine.

The old way,

#define code1 CTL_CODE(a, 3000, c, d)
#define code2 CTL_CODE(a, 3001, c, d)
#define code3 CTL_CODE(a, 3002, c, d)
....
#define code300 CTL_CODE(a, 3299, c, d)

Just seems like there should be an easier way since the only real thing
changing in each step is the function.

I guess I could use a simple enum and then wrap it with a function that sets
it up in the proper format but seems like a long way around ;/

Anyways, I was just wondering.

Thanks,
Jon
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
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.
 
J

Jon Slaughter

Jon Skeet said:
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).
 
J

Jon Slaughter

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)
 
J

Jon Slaughter

(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)
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[Flags] Enum -- make it better 1
Enumeration string representations 1
about enum 3
Enum TypeConverter 3
Question about enum values 10
Binding a list to an enumeration... 2
Spaces in Enum 11
Merge Info From Two Enums 1

Top