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.
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:
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.