开发者

c# overriding enum

I know that maybe this question has been asked before, but I can't seem to find a proper solution (having in mind that I am not a C# expert but a medium level user)...

I prepared a base class including an enum (AnimationStates) for animation states that my screen objects might have (for example a Soldier might have different states whereas a bird could have another set of states..) .. The base class is serving the purpose of storing update methods and other things for my animated screen objects (like animating all o开发者_运维问答f them in the same manner)... The enum in the base class is (naturally) empty inside.. I have methods written utilizing the enum members...

If I define enum in my child classes by "public new enum...", I can "inherit" it.. right ? Moreover, and interestingly, I have a Dictionary in base class and I am trying to pass it from a child (i.e. soldier, or bird) to its base (animatedobject) class ... But I can't..

I feel I am doing something wrong or missing.. Any ideas ?


Well, you cannot do it directly with enums in C#.

I would propose taking more object-oriented approach, and replace the enums with real objects. This way you define an interface IAnimationState in your base class, and add an abstract method getAnimationState() as well. In the screen object classes you just implement this method, returning some specific implementation of the interface IAnimationState. This way you can put some logic into the small animation state classes, making your project more modular.


You can't expand enums. You can create new enums in derived classes but they're distinct.

I think you should just use int constants.


An enumerated type represents a simple set of values. That's it. You are trying to use an enum as a class type when it simply doesn't fit the bill.

Just create an enum (if you actually need to) and make a "real" type for the complex operations.

enum SomeEnum { Foo, Bar }

class Whatever
{
    public SomeEnum { get { return SomeEnum.Foo; } }
}

This question is a good example of developing a solution without really understanding the problem. Instead of proposing your solution and asking us to figure out the last 20% that doesn't make any sense, tell us what you are actually trying to accomplish. Someone here may have a better approach that you haven't even thought of.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜