Solved

Static enum in C#

Posted on 2009-05-13
5
3,584 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 100 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 400 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
Expando 4 33
Windows Service to Receive TCP Packets 4 37
help with issues with ReportViewer in VS2015 5 22
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now