tags:

views:

842

answers:

4

I have an existing enum with numerous items in it.

I also have existing code which does certain things with this enum.

I would now like a way to view only a subset enum members. What I'm looking for is a way to divide my enum into groups. I need to preserve the (int) value of each member and I need to preserve the ability to view all enum members if needed.

The only thing I can think of is to just create a new enum for each subenum that only contain the items I want using the same name and value.

This works but violates the whole no repetition principle.

I don't expect anyone to have a better alternative but I thought I'd ask just in case someone had a fancy trick to show me.

Thanks, as always.

+1  A: 

You could define the values using an enumeration but then reference them via constants in static classes so that your developers don't get over-faced by the large enum. You could have:

enum MySuperEnumGroup
{
  Group1Item1,
  Group1Item2,
  Group1Item3,

  Group2Item1,
  Group2Item2,
  Group2Item3,

  Group3Item1,
  Group3Item2,
  Group3Item3,
}

static class MySuperEnumGroup_Group1
{
  public const MySuperEnumGroup Item1 = MySuperEnumGroup.Group1Item1;
  public const MySuperEnumGroup Item2 = MySuperEnumGroup.Group1Item2;
  public const MySuperEnumGroup Item3 = MySuperEnumGroup.Group1Item3;
}

static class MySuperEnumGroup_Group2
{
  public const MySuperEnumGroup Item1 = MySuperEnumGroup.Group2Item1;
  public const MySuperEnumGroup Item2 = MySuperEnumGroup.Group2Item2;
  public const MySuperEnumGroup Item3 = MySuperEnumGroup.Group2Item3;
}

//etc.
Jeff Yates
+1  A: 

This might help: Fake Enums in C#

olli
Thanks, that's an interesting article.
Jeff Yates
+1  A: 

In the end, I had to rewrite much of the code but the following "trick" was derived:

I trashed C# enums and use static members on a regular class. This class was made into a singleton and is inited on application start.

My static members' constructors are allowed to reference another static member as a "parent".

Next, my init method uses reflection to go through each static member and indexes them based on several properties. These indexes are stored in hashtables which are also members of the singleton.

I thus get:

a singleton object which:

  • has static members which can be easily accessed during design time.
  • can be used during run-time to lookup certain static members (based on "group" and other properties).

My init method does a fair amount of validation. If invalid (such as duplicate) static members are built, you get a run-time error on application startup.

Obviously a pretty big hack but I'm quite happy with it.

Mr Grieves
A: 

I would go with this (which works in VB.NET at least)

enum MySuperEnumGroup { Group1Item1, Group1Item2, Group1Item3,

Group2Item1, Group2Item2, Group2Item3,

Group3Item1, Group3Item2, Group3Item3, }

enum MySubEnumGroup { Group2Item1 = MySuperEnumGroup.Group2Item1 Group3Item1 = MySuperEnumGroup.Group3Item1 Group3Item3 = MySuperEnumGroup.Group3Item3 }

Then do some kind of CType when you need to.

ed_hubbell