0

So I have an enum like this:

public class MyObnoxiouslyLongClassName : AbstractModel, IPerforceModule
{
    public enum Status { Fatal, Priority, Error, Warning, Info, Debug }

and when I access the enum in another class I have to do so like:

MyObnoxiouslyLongClassName.Status state = MyObnoxiouslyLongClassName.Status.Fatal

What im trying to do is like link to this enum in my other class so I can skip the long class name.. something like this pseudocode:

public class MyOtherClass : AbstractModel, IFileBrowser
{
    private enum Status = MyObnoxiouslyLongClassName.Status
    Status state = Status.Fatal;
    (state == MyObnoxiouslyLongClassName.Status.Fatal) //true

Any way this could be done in C#?

Olli
  • 375
  • 5
  • 15
  • Closest you can do is define alias (but not inside `MyOtherClass` - on top of .cs file in which that class resides): `using Status = MyNamespace.MyObnoxiouslyLongClassName.Status;` – Evk Mar 27 '18 at 11:02
  • yep, alias did it, exactly what I need. Youre right this is probably a duplicate of the alias class name question, I just wasnt sure what doing this is called (alias) so was hard to search for. – Olli Mar 27 '18 at 12:19

1 Answers1

6

You have created an enum in the scope of the class. Simply move the enum out so 'it stands on its own' inside the namespace:

public enum Status { Fatal, Priority, Error, Warning, Info, Debug }

public class MyObnoxiouslyLongClassName : AbstractModel, IPerforceModule
{

Another way would be to create an using:

using Short = NameSpace.MyObnoxiouslyLongClassName.Status;

The downside of the latter is that you have to repeat the using in every class you intend to use it.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325