Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get global access to enum types in C#?

This is probably a stupid question, but I can't seem to do it. I want to set up some enums in one class like this:

public enum Direction { north, east, south, west };

Then have that enum type accessible to all classes so that some other class could for instance have:

Direction dir = north;

and be able to pass the enum type between classes:

public void changeDirection(Direction direction) {
   dir = direction;
}

I thought that setting the enum to public would make this automatically possible, but it doesn't seem to be visible outside of the class I declared the enum in.

like image 594
lala Avatar asked May 16 '10 18:05

lala


People also ask

Can enum be global?

If you really want a global, you can always make one. @mmmeff if you are in a module, you can use declare global to both define the availability of the enum at the type level and also make the assignment that provides the value typecheck.

Which datatype can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

Can an enum have an array?

To get all enum values as an array, pass the enum to the Object. values() method, e.g. const values = Object. values(StringEnum) .


4 Answers

You can do one of two things.

1- Move the declaration of the enum outside of the class

Today you probably have something like this

public class ClassName
{
  public enum Direction
  {
    north, south, east, west
  }
  // ... Other class members etc.
}

Which will change to

public class ClassName
{      
  // ... Other class members etc.
}

// Enum declared outside of the class
public enum Direction
{
  north, south, east, west
}

2- Reference the enum using the class name

ClassName.Direction.north

Eg.

public void changeDirection(ClassName.Direction direction) { 
   dir = direction; 
}

Where ClassName is the name of the class that you declared the enum in.

like image 183
Chris Taylor Avatar answered Oct 16 '22 20:10

Chris Taylor


Declare enum in the scope of a namespace like a class but not into a class:

namespace MyApplication
{
   public enum Direction { north, east, south, west };
}

In case enum is declared in the scope of a class, you have make this class public too:

namespace MyApplication
{
   public class MyClass
   {
      public enum Direction { north, east, south, west };
   }
}

Usage:

MyClass.Direction dir = ...
like image 39
abatishchev Avatar answered Oct 16 '22 19:10

abatishchev


It's public, but defining an enum inside a class makes it an inner type of that class. For instance:

namespace MyNamespace
{
    public class Foo
    {
        public enum MyEnum { One, Two, Three }
    }
}

In order to access this enum from another class in the MyNamespace namespace, you have to reference it as Foo.MyEnum, not just MyEnum. If you want to reference it as simply MyEnum, declare it just inside the namespace rather than inside the class:

namespace MyNamespace
{
    public class Foo { ... }

    public enum MyEnum { One, Two, Three }
}
like image 25
Adam Robinson Avatar answered Oct 16 '22 20:10

Adam Robinson


Put the enum definition inside of the Program.cs file, but outside of the Program class. This will make the enum type globally accessible without having to reference the class name.

namespace YourApp
{    
    enum Direction { north, east, south, west };

    static class Program
    {   
    }
}

Then you can access it anywhere in any class within the same namespace without the need to specify the class name like this:

Direction d;
d = Direction.north;
like image 38
SIRTAN Avatar answered Oct 16 '22 19:10

SIRTAN