Alternate enum value


K

kalvin.tuel

I have an enum marked with the flags attribute. I would like to
assign an alternate value to each enum member so that instead of being
stuck with the value needed for the flag I could give each member it's
position in the enum. This is just an example, what I really need is
to assign each value an ID from a database. Is there a builtin
attribute for this or do I need to create a custom attribute to handle
something like this?

What I would like:
[SYS.Flags]
private enum enumTest
{
[RealValue(1)]
First = 1,
[RealValue(2)]
Second = 2,
[RealValue(3)]
Third = 4
};

Thank you.
 
Ad

Advertisements

M

Mattias Sjögren

Is there a builtin
attribute for this or do I need to create a custom attribute to handle
something like this?

No built-in attribute that I know of.

[SYS.Flags]
private enum enumTest
{
[RealValue(1)]
First = 1,
[RealValue(2)]
Second = 2,
[RealValue(3)]
Third = 4
};

If all values follow the same pattern, you can calculate the "real
value" as

realValue = Math.Log((int)yourEnumTestValue, 2) + 1;


Mattias
 
K

kalvin.tuel

Is there a builtin
attribute for this or do I need to create a custom attribute to handle
something like this?

No built-in attribute that I know of.
[SYS.Flags]
private enum enumTest
{
[RealValue(1)]
First = 1,
[RealValue(2)]
Second = 2,
[RealValue(3)]
Third = 4
};

If all values follow the same pattern, you can calculate the "real
value" as

realValue = Math.Log((int)yourEnumTestValue, 2) + 1;

Mattias

Thanks a lot. I'll give that a try.
 
Ad

Advertisements

B

Ben Voigt [C++ MVP]

Mattias Sjögren said:
Is there a builtin
attribute for this or do I need to create a custom attribute to handle
something like this?

No built-in attribute that I know of.

[SYS.Flags]
private enum enumTest
{
[RealValue(1)]
First = 1,
[RealValue(2)]
Second = 2,
[RealValue(3)]
Third = 4
};

If all values follow the same pattern, you can calculate the "real
value" as

realValue = Math.Log((int)yourEnumTestValue, 2) + 1;

A lookup table should be much faster and not use that much memory, plus
there needn't be any pattern.
 

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


Top