?
Solved

Static enum in C#

Posted on 2009-05-13
5
Medium Priority
?
3,610 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

801 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