0

I'd like to declare an enum (a bitfield) in a base class and add to that list in subclasses:

public class BaseClass
{
  public virtual enum Operations 
  {
    Hello = Math.Pow(2,0),
    GoodBye = Math.Pow(2,1)
  }

  public Operations ValidThingsToSay()
  {

  }
}

public class PoliteClass : BaseClass
{
  public override enum Operations : base()
  {
    ThankYou = Math.Pow(2,2),
    Please = Math.Pow(2,3)
  }
}

public class TouristClass : PoliteClass
{
  public override enum Operations : base()
  {
    WheresTheBathroom = Math.Pow(2,4),
    HowMuchIsThat = Math.Pow(2,5)
  }
}

foreach(var c in MyClasses)
 Console.WriteLine(c.ValidThingsToSay().ToString());

What's the closest clean way to do this that may still have some of the benefits of using enums (intellisense, bitfield results assignable to same type, etc.)?

You're going to say use constants and a list, aren't you?

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    2^3? That does not do what you think ... https://msdn.microsoft.com/en-us/library/zkacc7k1.aspx – Thomas Weller Feb 23 '17 at 20:21
  • `2^2`, `2^5`?? Why *XOR* `^`? You, probably, mean `1 << 2`, `1 << 5`... – Dmitry Bychenko Feb 23 '17 at 20:21
  • Math.Pow() is a little clearer but still won't compile, never mind that. – H H Feb 23 '17 at 20:30
  • 1
    Sadly, just as this was closed I was just about to hit "submit" on an idea for some code that would, I think, do what you're trying to do here. – 15ee8f99-57ff-4f92-890c-b56153 Feb 23 '17 at 20:31
  • If you can edit this question away from asking for Enum inheritance, I believe I have a solution that can be adapted to achieve your end goal –  Feb 23 '17 at 20:31
  • @EdPlunkett - If you feel you have something useful you can always Ask and post a self-answer. Or Edit&improve this question. – H H Feb 24 '17 at 08:26
  • 1
    @EdPlunkett, I put my answer [here](http://stackoverflow.com/questions/757684/enum-inheritance/42446664#42446664) – toddmo Feb 24 '17 at 19:24
  • @toddmo Cool. I looked through the other answers there, and it looks like mine was a very slight refinement of [this one](http://stackoverflow.com/a/4042826/424129) -- I overrode `ToString` and added `explicit operator int()` – 15ee8f99-57ff-4f92-890c-b56153 Feb 24 '17 at 19:28

0 Answers0