28

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.

2
  • I don't think there's a way to do what you want (it depends on what you mean by "view"). But if you can describe the bigger picture, perhaps there's a better way to accomplish your goal?
    – colithium
    Feb 5, 2009 at 23:21
  • The point of my enum is to allow fellow developers to choose a certain value based on its name. My enum is becoming too large and sometimes it would be good if the developers could only see a subset of enum members based on a certain grouping. That grouping is fixed.
    – srmark
    Feb 5, 2009 at 23:27

5 Answers 5

20

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.

1
  • I just upvoted an answer someone with my name made on some unregistered account 11 years ago in a language rarely code in anymore. I honestly don't know if it was me or not. But it did fix my issue.
    – edhubbell
    Mar 24, 2021 at 18:01
11

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.
1
  • 1
    how to view all enum members?
    – Mhd
    Jun 7, 2017 at 15:46
6

If the enums don't have explicit values, give them one and use flags to define the "groupings":

[Flags]
enum MySuperEnumGroup
{
    Group1 = 1 << 0,
    Group2 = 1 << 1,
    Group3 = 1 << 2,

    Group1Item1 = 1 << 10 | Group1,
    Group1Item2 = 1 << 11 | Group1,
    Group1Item3 = 1 << 12 | Group1,

    Group2Item1 = 1 << 13 | Group2,
    Group2Item2 = 1 << 14 | Group2,
    Group2Item3 = 1 << 15 | Group2,

    Group3Item1 = 1 << 16 | Group3,
    Group3Item2 = 1 << 17 | Group3,
    Group3Item3 = 1 << 18 | Group3,
}

Then you can use Enum.GetValues and HasFlag to get the values for a given "grouping":

var group1 = Enum.GetValues(typeof(MySuperEnumGroup))
                 .Cast<MySuperEnumGroup>()
                 .Where(value => value.HasFlag(MySuperEnumGroup.Group1))
                 .ToArray();
2
  • I like this approach and I'm using a modified version of it where the group identifiers start at bit position 16 and up, and the items within the groups go from bit position 0, 1, 2, etc. along with the or'd group identifier as you are. Did you have a reason to make the shift positions unique across the groups? Do you see an issue with what I am doing where they are not unique (without or-ing the group)? Nov 18, 2019 at 17:33
  • 1
    @ChristopherKing as long as bit math works, you're good. Note that shifting beyond << 31 will overflow an int, so this works best for small sets of enum constants. If you have a lot, you might want to use another approach, or making the enum : Int64, but then that's not optimal. Nov 18, 2019 at 17:52
3

This might help: Fake Enums in C#

0
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.