Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. new Enum

new Enum

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dean Goodman
    wrote on last edited by
    #1

    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 as new in the derived class I've created a totally new type; thus the compiler complains that I shouldn't be trying to override ValidateAction in DerivedClass. Basically I want to avoid having to use protected bool ValidateAction(int) as my function prototype, thereby avoiding having to cast my enum values to ints all the time to make the call to ValidateAction. Also, this would allow me to call ValidateAction from BaseClass and be sure that my virtual method in DerivedClass gets called. Is there an obvious way of doing this that I am missing? Deriving from System.Enum to create an Action base class would do the trick, but I've read somewhere that this is poor practice. Any other suggestions?

    T 1 Reply Last reply
    0
    • D Dean Goodman

      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 as new in the derived class I've created a totally new type; thus the compiler complains that I shouldn't be trying to override ValidateAction in DerivedClass. Basically I want to avoid having to use protected bool ValidateAction(int) as my function prototype, thereby avoiding having to cast my enum values to ints all the time to make the call to ValidateAction. Also, this would allow me to call ValidateAction from BaseClass and be sure that my virtual method in DerivedClass gets called. Is there an obvious way of doing this that I am missing? Deriving from System.Enum to create an Action base class would do the trick, but I've read somewhere that this is poor practice. Any other suggestions?

      T Offline
      T Offline
      TylerBrinks
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups