C
Claire
I have a large record with many enumerated fields.
The record is stored in a file and the fields have to be extracted.
I validate the data as it's read, but there's so many tests similar to the
following that I wondered if it's possible to create a single generic
function to perform the validation. Just to keep code size down if nothing
else. I'm not that experienced with .net yet so I don't know what's
possible.
example code
reader is a binaryreader object, all the different enumerated types have an
"Invalid" member which is the final member of the enumeration.
// BaudRate
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eBaudRate.Invalid))
baudRate = eBaudRate.Invalid;
else
baudRate = (eBaudRate)Enum;
//Parity.
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eParity.Invalid))
parity = eParity.Invalid;
else
parity = (eParity)Enum;
// WordLength.
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eWordLength.Invalid))
wordLength = eWordLength.Invalid;
else
wordLength = (eWordLength)Enum;
The record is stored in a file and the fields have to be extracted.
I validate the data as it's read, but there's so many tests similar to the
following that I wondered if it's possible to create a single generic
function to perform the validation. Just to keep code size down if nothing
else. I'm not that experienced with .net yet so I don't know what's
possible.
example code
reader is a binaryreader object, all the different enumerated types have an
"Invalid" member which is the final member of the enumeration.
// BaudRate
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eBaudRate.Invalid))
baudRate = eBaudRate.Invalid;
else
baudRate = (eBaudRate)Enum;
//Parity.
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eParity.Invalid))
parity = eParity.Invalid;
else
parity = (eParity)Enum;
// WordLength.
Enum = reader.ReadInt16();
if ((Enum < 0)||(Enum > (byte)eWordLength.Invalid))
wordLength = eWordLength.Invalid;
else
wordLength = (eWordLength)Enum;