There are several threads in ranges of values ββin enumerations (this is not possible). But I have the following problem and the search for a better solution, where none of the ones provided to me satisfied me.
The protocol specification says that byte [x] of the message, the type of message, has the following possible values ββ(fantasy values):
0x00 = get 0x01 = set 0x02 to 0xFF = identify
Thus, there are only 3 different logical options that will best be considered in the listing. But one of the n logical options has m different numerical copies with which it is impossible to translate into an enumeration.
Now what is the best (cleanest) solution for such a problem? I could create a class
class MessageType { public enum MessageTypeEnum { get = 0x00, set = 0x01, identify = 0x02 } public static MessageTypeEnum getLogicalValue (byte numericalValue) { if (numericalValue < 0x02) return (MessageTypeEnum(numericalValue)); else return MessageTypeEnum.identify; } }
I could also create a class without enum, but with static members.
In any case, one problem arises. If someone tries to send a packet, he can use
if (messageBytes[x] == (byte)MessageTypeEnum.identify) {
But messageByte [x] can be anything between 0x02 and 0xFF, so "hit" the value specified in the enumeration would be pure luck. On the other hand, I want the enum (or static member) to be publicly available for easy posting.
Can I somehow use the use of my getLogicalValue () function - Function? Is there a more elegant solution?
All I want is a simple and well-structured way of associating logical values ββwith numerical values ββwith respect to n: m. Moreover, this protocol has many such cases, and I would like to keep my code neat.
Thanks for your help and time :)
Janis