enumerations and out of scope values

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

I have an enumeration of constants as follows.

public enum eDeviceErrors
{
NoError = 0,
ENOENT = 2,
EBADF = 9,
EACCES = 13,
EINVAL = 22,
Checksum = 40,
SeqNum = 41,
InvalidTour = 42,
CommandNotRecognized = 43,
FOPEN = 44,
LENGTH = 45,
TERMINATOR = 46,
FWRITE = 47,
FCLOSE = 48,
FGETC = 49,
InvalidCode = 100
}

If I read a byte from the comport and want to assign this byte to an
enumerated variable, yet catch the case where the value isn't within scope
without testing against each possible value, how do I do this please?
eg
eDeviceErrors Errors = MyComport.Read(1);
 
You may use System.Enum.IsDefined:
if (! Enum.IsDefined(typeof(eDeviceErrors), value)
{
throw ...;
}
eDeviceErrors e = (eDeviceErrors) value;
 
Back
Top