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.
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.
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.
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
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 = ...