Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Static enum in C#

Posted on 2009-05-13
5
Medium Priority
?
3,634 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:Yurich
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24379726
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.
0
 
LVL 6

Assisted Solution

by:ViceroyFizzlebottom
ViceroyFizzlebottom earned 400 total points
ID: 24379738
Also, you can define a static instance of your enum, so you could say:

public static myEnum mine = myEnum.Option3;
0
 
LVL 9

Accepted Solution

by:
tculler earned 1600 total points
ID: 24380585
Enumerations ("enums") are just sets of named constants, and constants are ALWAYS static in C#. The access modifier is all that should concern you. In other words, you cannot declare an enumeration as static--it is static implicitly, just like an instance modified with the keyword "const".

Accessing an enumeration is just like accessing the static members of a type. Just put the name of the enumeration, followed by the enumeration's member you wish to access. If you put an enumeration declaration within a class, you must specify the class, too. For example, to access the enumeration SomeEnum provided below...

// File: NestedEnum.cs (or whatever)
namespace SomeNamespace
{
     public class SomeClass
     {
          public enum SomeEnum { One, Two, Three, Four /*Etc*/ };
           // SomeClass members and such
     }
}

// File: EnumAccess_Main.cs (or whatever)
namespace SomeOtherNamespace
{
     public class Startup
     {
          public static void main()
          {
               SomeMethodThatNeedsSomeEnum(SomeNamespace.SomeClass.SomeEnum.One);
               // or, if you put "using SomeNamespace;" at the very top of the file, you can just say:
               SomeMethodThatNeedsSomeEnum(SomeClass.SomeEnum.One);
          }
     }
}

This is one way. However, it's easier to put the enum definition in the namespace and not within a class. See below.

// File: NestedEnum.cs (or whatever)
namespace SomeNamespace
{          
     public enum SomeEnum { One, Two, Three, Four /*Etc*/ };
     public class SomeClass
     {
           // SomeClass members and such
     }
}

// File: EnumAccess_Main.cs (or whatever)
namespace SomeOtherNamespace
{
     public class Startup
     {
          public static void main()
          {
               SomeMethodThatNeedsSomeEnum(SomeNamespace.SomeEnum.One);
               // or, if you put "using SomeNamespace;" at the very top of the file, you can just say:
               SomeMethodThatNeedsSomeEnum(SomeEnum.One);
          }
     }
}

Hope I helped,
Nate
0
 
LVL 9

Expert Comment

by:tculler
ID: 24380599
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?
0
 
LVL 21

Author Comment

by:Yurich
ID: 24380950
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question