Solved

Static enum in C#

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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