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?

8
  • 1
    2^3? That does not do what you think ... msdn.microsoft.com/en-us/library/zkacc7k1.aspx Commented Feb 23, 2017 at 20:21
  • 2^2, 2^5?? Why XOR ^? You, probably, mean 1 << 2, 1 << 5... Commented Feb 23, 2017 at 20:21
  • Math.Pow() is a little clearer but still won't compile, never mind that. Commented Feb 23, 2017 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. Commented Feb 23, 2017 at 20:31
  • 1
    @EdPlunkett, I put my answer here Commented Feb 24, 2017 at 19:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.