title: C# alternative to enums for a n:m-relation
body: <p>there are several threads on value-ranges in enums (not possible).
  But I have the following problem and search the best solution, where none of the provided once really satisfied me.</p>
  
  <p>A specification of a protocol says that byte[x] of a message, the messagetype, has the following possible values (fantasy values): </p>
  
  <pre><code>0x00 = get
  0x01 = set 
  0x02 to 0xFF = identify
  </code></pre>
  
  <p>So there are only 3 different logical options, which would best be dealt with in an enum. But one of the n logical options has m different numerical counterparts, which is impossible to be dealt with in an enum.</p>
  
  <p>Now what is the best (cleanest) solution for such a problem?
  I could build a class</p>
  
  <pre><code>class MessageType {
      public enum MessageTypeEnum {
          get = 0x00, 
          set = 0x01, 
          identify = 0x02
      }
  
      public static MessageTypeEnum getLogicalValue (byte numericalValue)
      {
         if (numericalValue &lt; 0x02) 
            return (MessageTypeEnum(numericalValue)); 
         else
            return MessageTypeEnum.identify;
      }
  }
  </code></pre>
  
  <p>I could also create a class without an enum, but with static members.</p>
  
  <p>Either way there is one Problem: If someone tries to dispatch a packet, he might use </p>
  
  <pre><code>if (messageBytes[x] == (byte)MessageTypeEnum.identify) {
      // do stuff
  }
  </code></pre>
  
  <p>But messageByte[x] could be anything between 0x02 and 0xFF, so "hitting" the value specified in the enum would be pure luck. On the other side I want the enum (or static member) to be public for easy message-building. </p>
  
  <p>Can I somehow enforce the use of my getLogicalValue()-Function?
  Is there a more elegant solution?</p>
  
  <p>All I want is an easy and well-structured way to link logical values to numerical values in a n:m relation. Especially as the given protocol has many such cases and I would like to keep my code neat.</p>
  
  <p>Thanks for your help and time :)</p>
  
  <p>Janis</p>