tags:

views:

1553

answers:

8

I want to declare a nested enum like:

\\pseudocode
public enum Animal
{
  dog = 0,
  cat = 1
}

private enum dog
{
   bulldog = 0,
   greyhound = 1,
   husky = 3
}

private enum cat
{
   persian = 0,
   siamese = 1,
   burmese = 2
}

Animal patient1 = Animal.dog.husky;

Can it be done?

+6  A: 

Simply, no, it cannot.

I recommend that you define all of the values within the Animal enum. Is there any reason why you want this particular structure?

Noldorin
I had it like that, but wanted to subdivide the main enum into the underlying different groups.
callisto
I did something very similar. I added a support class with a static member function that would take an enum value (husky, persian, etc.) and return another enum value (dog or cat). Most of the code kept the original enum, but where I wanted to organize things for the user, I could easily get whether the value was a cat or a dog.The support class had a static dictionary with the mapping of husky=>dog, etc.
NascarEd
A: 

Switch to Java and it can be :)

enum Animal { enum Dog { BARK, WAG }; };

amischiefr
wow, that was fast (about 30 secs). thanks.
callisto
It is a C# project.
callisto
+1  A: 

I don't think it works that way.

Enumerations are supposed to be a simple set of parallel values.

You may want to express that relationship with inheritance.

lyxera
please elaborate...
callisto
If the only information he needs to express is the *type* of animal, the creating a class hierarchy with inheritance probably isn't the best idea.
Noldorin
This is so not the OO way of doing things. Why would you not be doing this with class inheritance? animal1 sounds like an object to me...
annakata
if *type* of animal is just a value, why bother the hierarchy at the first place?
lyxera
The actual enums are Editjob, AddJobcard, EditInstallation, AddInstallation etc. Used in a mobile app to switch shared usercontrols' controls active/inactive and some places set visibilty of controls
callisto
+5  A: 

You can use this method to get what you want though

public static class Animal {
    public enum Dog {
        BullDog,
        GreyHound,
        Huskey
    }

    public enum Cat {
        Tabby,
        Bombbay
    }
}
Nick Berardi
This is a fair suggestion, though I still isn't how I would choose to do it. It gives you the right syntax, but not the necessary inheritance. As far as the CLR is concerned, Dog and Cat are totally unrelated. Simply defining all the values in a single Animal enum shows that they are homogeneous in some form (i.e. all animals). Consider: how would you pass either a Dog *or* a Cat as a parameter?
Noldorin
A: 

See these questions:
http://stackoverflow.com/questions/940002/getting-static-field-values-of-a-type-using-reflection
http://stackoverflow.com/questions/958606/storing-string-values-as-constants-in-the-same-manner-as-enum

The questions cover building a basic string enum, but I implement my answers using an ICustomEnum<T> interface that might help you in this situation.

Joel Coehoorn
A: 

Perhaps this would suffice?

class A
{
  public const int Foo = 0;
  public const int Bar = 1;
}

class B : A
{
  public const int Baz = 2;
}
leppie
+5  A: 

I would probably use a combination of enumerated bit fields and extension methods to achieve this. For example:

public enum Animal
{
   None = 0x00000000,
   AnimalTypeMask = 0xFFFF0000,
   Dog = 0x00010000,
   Cat = 0x00020000,
   Alsation = Dog | 0x00000001,
   Greyhound = Dog | 0x00000002,
   Siamese = Cat | 0x00000001
}

public static class AnimalExtensions
{
  public bool IsAKindOf(this Animal animal, Animal type)
  {
    return (((int)animal) & AnimalTypeMask) == (int)type);
  }
}

Update
In .NET 4, you can use the Enum.HasFlag method rather than roll your own extension.

Jeff Yates
A: 

how do you use this function in code?

public bool IsAKindOf(this Animal animal, Animal type)
var myPet = Animal.BullDog;if (myPet.IsAKindOf(Animal.Dog)) {} else {}
Torbjörn Hansson