Reputation: 3515
If I have in my Parent class property ConditionStatus of datatype ConditionEnum like
interface IArticle
{
ConditionEnum ConditionStatus {get; set;}
}
enum is represent like ConditionEnum {New, Used};
I'm wonder how can I change this ConditionEnum
in child class so that ArticleA
which implements IArticle
can have enum like ConditionEnum {Red, Blue, Yellow};
and ArticleB
which also implements IArticle
have enum
like ConditionEnum {Point, Comma, Slash};
I think you've got an idea.
How would you do it?
Upvotes: 2
Views: 113
Reputation: 1500785
You could make your interface generic:
public interface IArticle<T> where T : struct
{
T ConditionStatus { get; set; }
}
public class ArticleA : IArticle<ColorEnum>
{
public ColorEnum ConditionStatus { get; set; }
}
public class ArticleB : IArticle<PunctuationEnum>
{
public PunctuationEnum ConditionStatus { get; set; }
}
Note that you can't enforce that T
is an enum... and you do need to specify which enum your implementation will use. It's not entirely clear whether that's going to help you or not, but it's about all there is...
Upvotes: 4
Reputation: 35842
Since enums
can't derive from other enum
types or interfaces, it's almost impossible. What you describe seems polymorphism to me. But AMAIK, you can't apply polymorphism to enums.
Upvotes: 2
Reputation: 33521
enum
inside a class, you'd refer to it as ClassA.ConditionEnum.*
:.
class MyClass : IArticle {
public enum ConditionEnum { Red, Blue, Yellow };
public ConditionEnum myenum;
}
MyClass c = new MyClass();
c.myenum = MyClass.ConditionEnum.Red;
Upvotes: 0