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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top