new Enum
-
I'm looking for a way to do the following:
class BaseClass { public enum Action { NO_ACTION } protected virtual bool ValidateAction(Action a) { return true; } } class DerivedClass : BaseClass { public new enum Action { ACTION1, ACTION2 } protected override bool ValidateAction(Action a) { //... validate Action ... } }
The above doesn't work because by declaring my
enum Action
asnew
in the derived class I've created a totally new type; thus the compiler complains that I shouldn't be trying to overrideValidateAction
inDerivedClass.
Basically I want to avoid having to useprotected bool ValidateAction(int)
as my function prototype, thereby avoiding having to cast my enum values to ints all the time to make the call toValidateAction
. Also, this would allow me to callValidateAction
fromBaseClass
and be sure that my virtual method inDerivedClass
gets called. Is there an obvious way of doing this that I am missing? Deriving fromSystem.Enum
to create anAction
base class would do the trick, but I've read somewhere that this is poor practice. Any other suggestions? -
I'm looking for a way to do the following:
class BaseClass { public enum Action { NO_ACTION } protected virtual bool ValidateAction(Action a) { return true; } } class DerivedClass : BaseClass { public new enum Action { ACTION1, ACTION2 } protected override bool ValidateAction(Action a) { //... validate Action ... } }
The above doesn't work because by declaring my
enum Action
asnew
in the derived class I've created a totally new type; thus the compiler complains that I shouldn't be trying to overrideValidateAction
inDerivedClass.
Basically I want to avoid having to useprotected bool ValidateAction(int)
as my function prototype, thereby avoiding having to cast my enum values to ints all the time to make the call toValidateAction
. Also, this would allow me to callValidateAction
fromBaseClass
and be sure that my virtual method inDerivedClass
gets called. Is there an obvious way of doing this that I am missing? Deriving fromSystem.Enum
to create anAction
base class would do the trick, but I've read somewhere that this is poor practice. Any other suggestions?By definition an enumeration is not subject to change. Here's a definition right off of MSDN. "Constants provide a convenient way to set and refer to values that are not expected to change." Your enum should define all actions that the base and all sub classes can perform. Your sub class doesn't have to handle all the actions, but it does need to use the same enumeration.