How can I define an enumeration where multiple values map to a single label?
Asked Answered
V

2

10

Suppose, for the sake of this example, that I am trying to parse a file which specifies that two arbitrary bytes in the record represent the day of the week, thusly:

DayOfWeek:

 - 0    = Monday
 - 1    = Tuesday
 - 2    = Wednesday
 - 3    = Thursday
 - 4    = Friday
 - 5    = Saturday
 - 6    = Sunday
 - 7-15 = Reserved for Future Use

I can define an enumeration to map to this field, thusly:

public enum DaysOfWeek
{
     Monday = 0,
     Tuesday = 1,
     Wednesday = 2,
     Thursday = 3,
     Friday = 4,
     Saturday = 5,
     Sunday = 6
     ReservedForFutureUse
}

But how can I define valid values for ReservedForFutureUse? Ideally, I'd like to do something like:

public enum DaysOfWeek
    {
         Monday = 0,
         Tuesday = 1,
         Wednesday = 2,
         Thursday = 3,
         Friday = 4,
         Saturday = 5,
         Sunday = 6
         ReservedForFutureUse = {7,8,9,10,11,12,13,14,15}
    }

This problem is only exacerbated with more complicated fields; suppose, for example, that both 7 and 8, in this case, map to the same error case or something. How can one capture this requirement in a C# enumeration?

Vatican answered 11/3, 2013 at 14:59 Comment(5)
The problem with this: how would it work going the other direction? Going from the int value to the enum would work but how would c# be able to know which int value the enum was supposed to be cast to if you do it the other way?Stanwinn
I guess the type-safe-enum pattern should be of interest. Have a look at : https://mcmap.net/q/53312/-string-representation-of-an-enumRonn
Can you describe how you are going to use this 'Reserved' value? You will change enum in future, so why would you add some value which will be changed (not necessary to one of listed values)Sizzle
In this particular case, its just an example; in the actual problem, there are actually several fields that could be one of several values.Vatican
Here's another answer that dynamically generates an enum assembly - https://mcmap.net/q/167459/-automatically-create-an-enum-based-on-values-in-a-database-lookup-table. Interesting stuff. The premise of an enum with "reserved" values is counter-intuitive to the concept of an enum. At most if you have a database with lots of lookup tables, generating an enum DLL can help save typing... but to dynamically generate one on the fly would make usage and debugging quite difficult.Siouan
F
5

One funny quirk with enums is that a variable defined as a certain enum type can hold values that are not defined by any member of that enum:

public enum DaysOfWeek
{
     Monday = 0,
     Tuesday = 1,
     Wednesday = 2,
     Thursday = 3,
     Friday = 4,
     Saturday = 5,
     Sunday = 6
}

// in other code
DaysOfWeek someDay = (DaysOfWeek)42; // this is perfectly legal

This means that you don't really need to define all possible values that can appear, but can rather just specify those that mean something to your code. Then you can use some "catch-all" if- or switch-block to handle undefined values:

switch (someDay)
{
    case DaysOfWeek.Monday:
    {
        // do monday stuff
        break;
    }
    case DaysOfWeek.Tuesday:
    {
        // do tuesday stuff
        break;
    }
    // [...] handle the other weekdays [...]
    default:
    {
        // handle undefined values here
        break;
    }
}
Fennelly answered 11/3, 2013 at 15:27 Comment(0)
V
3

Although a given underlying value may be mapped to multiple enum values, an enum value can have exactly one underlying value.

You could just do this

public enum DaysOfWeek
{
     Monday = 0,
     Tuesday = 1,
     Wednesday = 2,
     Thursday = 3,
     Friday = 4,
     Saturday = 5,
     Sunday = 6
     Reserved1 = 7
     ...
     Reserved8 = 15
}
Vd answered 11/3, 2013 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.