[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Static enum in C#

Posted on 2009-05-13
5
Medium Priority
?
3,626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

650 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