i have problem with enum
I need make a enum in base class or interface (but empty one)
class Base
{
public enum Test;
// ???
}
and after make diffrent enums in some parent classes
class Parent1
{
public enum Test {A, B, C};
}
class Parent2
{
public enum Test {J, H, K};
}
and now i have next class with method when i have to use enum
class Test<T>
{
public void Foo(Test enum)
{
int value = (int) enum;
// ...
}
}
It's there any way to do something like that ?
If not i have to use static ints in every class ...
class Parent1
{
public static int A = 0;
public static int B = 5;
public static int C = 7;
}
class Parent2
{
public static int J = 1;
public static int H = 3;
public static int K = 6;
}
class Test<T>
{
public void Foo(int enum)
{
int value = enum;
// ...
}
}
I't looks bad in code ... in some classes i have to use ~20+ variables
There is no such thing as an abstract enum (that can have different implementations in subclasses) - but generics may be an option:
The only problem is that this doesn't enforce that only enums are usable - you can do this at runtime, though - for example in a type initializer:
I like the accepted answer of @Marc Gravell. As i am a stackoverflow-newbie i am not allowed to comment. But i would like to add, that it is useful to also check the underlying Type of the enum - especially if you use the Flag Attribute and preform bit-wise flag-Testing operations...
I am at work so cannot elaborate fully, but this is possible to a point. The downside to the code below is you cannot chain enums together, such as TextBoxProperties, and MyCustomTextBoxProperties : TextboxProperties
Here is the code.
YOU CAN ABSTRACT AN ENUM!
Why do people insist on claiming things are impossible without checking anything?
Sure, the .NET documentation is a little vague on this, but the System.Enum class, IS the abstraction of an enum. You can use System.Enum as a variable that will ONLY accept enumeration values, and you can access the name or value-type value of the enumeration through this class.
For example:
The output from the above code:
9
9
Blue
Here's a solution that works for me:
in the parent class, declare the field as int, not as enum:
so the parent class can still use this value as an int, in order to provide serialization and deserializion of this value to disk.
Then, who overrides the class can do this:
and can access the actual value like this:
where toInt() is defined in a static class as follows:
You should be able to declare an enum in a base class and then change the values per derived class i.e.
MyDervied class would have access to TestEnum.value1, TestEnum.value2, TestEnum.value3, where as MyClass would only have access to the type.
However, personally I don't see the advantage of doing this I would declare ALL the values of the enum in the base class and only use the ones I need per class.
James.