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?

GWLlosa
  • 23,995
  • 17
  • 79
  • 116
  • 2
    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? – jzworkman Mar 11 '13 at 15:04
  • I guess the type-safe-enum pattern should be of interest. Have a look at : http://stackoverflow.com/a/424414/1236044 – jbl Mar 11 '13 at 15:11
  • 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) – Sergey Berezovskiy Mar 11 '13 at 15:11
  • In this particular case, its just an example; in the actual problem, there are actually several fields that could be one of several values. – GWLlosa Mar 11 '13 at 15:13
  • Here's another answer that dynamically generates an enum assembly - http://stackoverflow.com/a/792332/546000. 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. – Dmitriy Khaykin Mar 11 '13 at 15:13

2 Answers2

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;
    }
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
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
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331