-3

Is there a way to reuse enums in other classes?

For example, I have a class using another class's enums. However, I want to avoid having to type the other class's namespace every time I use that class's enums for my active class.

user3080777
  • 3
  • 1
  • 6

3 Answers3

0

Is there a good reason why you can't just use a using statement?

using Namespace.With.Enum;

namespace Namespace.Without.Enum 
{
    public class Foobar 
    {
        ...
    }
}

This should allow you to use the enum inside Foobar without needing to preface it.

pymaxion
  • 393
  • 3
  • 8
0

I don't see the issue with putting a using statement at the top of each file.

 namespace MyNameSpace.EnumNamespace
 {
    public enum MyEnum
    {
         One,Two,Three
    }
 }

And in a separate file

using MyNamespace.EnumNamespace;


namespace AnotherNamespace
{
        public class AnotherClass
        {
            //somewhere in the same file
            public void SomeMethod(MyEnum enumValue)
            {
                //Do stuff
            }
        }
}

Alternatively, you can also assign an alias as well.

using MyEnumAlias = MyNameSpace.EnumNamespace.MyEnum
konkked
  • 3,161
  • 14
  • 19
0

If you just don't want put namespace and class name, e.g.

  namespace AnotherNamespace {
    ...
    public class AnotherClass {
      ...
      public enum MyEnum {
        One, 
        Two,
        Three 
     }
    }
    ...
  }

  ...

  namespace MyNamespace { 
    ...
    AnotherNamespace.AnotherClass.MyEnum v = ...

you can try creating synonym via using:

  namespace MyNamespace {
    using MyEnum = AnotherNamespace.AnotherClass.MyEnum;

    ...

    MyEnum v = ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215