Link to home
Start Free TrialLog in
Avatar of Yurich
YurichFlag for New Zealand

asked on

Static enum in C#

Hello,
I need to create some sort of static enumeration, but it's not allowed in C#, e.g. if I try the following:

static public enum  myEnum{ Option1 = 1, Option2, Option3 };

it will error out. I need to have it static as I do my calculations in different classes that interact with each other and one piece of a data is an array (i.e. int[] arOptions ). So when I use this array, I don't want to do this:

arOptions[ 3 ] = ...

as if I later decide to change meaning of #3, it will be a nightmare to change all references, instead, I want to do

arOptions[ Option3 ] = ...

And the only way to use enums now is to define the same enum in each class, but that's not good programming either.

So, how can and can I employ strong typing in my case?

Thanks
Avatar of ViceroyFizzlebottom
ViceroyFizzlebottom
Flag of United States of America image

You can define the Enum in it's own file under a specific namespace. Then for every file which references it, you can simply include a #using <whatever> to get access. It will also allow you to only change the specific values in only one place.
SOLUTION
Avatar of ViceroyFizzlebottom
ViceroyFizzlebottom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, one more thing. Remember that your enumeration will start numbering from your first specified value. If you make your first value = 1, choosing the "third" member will access the forth element of the array (aka, index 3, but "slot" 4). Was this intentional?
Avatar of Yurich

ASKER

2 Nate,
Very elaborated answer, thanks a lot. Dropping a static keyword worked as suggested. Using value = 1 was intentional as 0-slot was used for maintenance and in my case counting from 1 was easier than from 0.

Thanks a lot,
Yurich