473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Writing a generic function to validate values translating to enums

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;
Nov 16 '05 #1
3 3310
This is an example of how it could be done.

It's using reflection so performance is a bit down, but this could be
optimized a bit. For example the max values could be read just once and
stored in a hashtable and the enumtypes could be stored in variables.

static void Main(string[] args)
{
// BaudRate
Enum = reader.ReadInt16();
baudrate = (eBaudRate)Validate(Enum, typeof(eBaudRate));

//Parity.
Enum = reader.ReadInt16();
parity = (eParity)Validate(Enum, typeof(eParity));

}

public static int Validate(int16 value, type enumType)
{
int max = (int)(Enum.Parse(enumType, "Invalid"));
bool valid = !( value < 0 || value > max );

if (valid)
return value;
else
return max;
}

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Claire wrote:
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;

Nov 16 '05 #2
because you're repeating the "reader.ReadInt16();", you could pass in a
valid reader reference to the function...

// BaudRate
baudrate = (eBaudRate)Validate(reader, typeof(eBaudRate));

//Parity.
parity = (eParity)Validate(reader, typeof(eParity));


public static int Validate(BinaryReader reader, type enumType)
{
int readValue = reader.ReadInt16()
int max = (int)(Enum.Parse(enumType, "Invalid"));
bool valid = !( readValue < 0 || readValue > max );

if (valid)
return readValue ;
else
return max;
}
""Patrik Löwendahl [C# MVP]"" <pa******************************@home.se>
wrote in message news:u1**************@TK2MSFTNGP09.phx.gbl...
This is an example of how it could be done.

It's using reflection so performance is a bit down, but this could be
optimized a bit. For example the max values could be read just once and
stored in a hashtable and the enumtypes could be stored in variables.

static void Main(string[] args)
{
// BaudRate
Enum = reader.ReadInt16();
baudrate = (eBaudRate)Validate(Enum, typeof(eBaudRate));

//Parity.
Enum = reader.ReadInt16();
parity = (eParity)Validate(Enum, typeof(eParity));

}

public static int Validate(int16 value, type enumType)
{
int max = (int)(Enum.Parse(enumType, "Invalid"));
bool valid = !( value < 0 || value > max );

if (valid)
return value;
else
return max;
}

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Claire wrote:
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;

Nov 16 '05 #3
That works great. Thanks Patrik and Dan

Claire
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Sworder | last post by:
With a standard enum, values are automatically assigned. public enum MyEnum{Happy,Sad,Angry,Grandpa}; ....but what about a bitflag enum? public enum MyEnum:int{ FalseTeeth=0x00000001...
6
by: hharry | last post by:
Hello All, Is it possible to force a function argument to fall with a range of values ? For example: I have a function which searches a database table and accepts a query type argument...
2
by: Stefan L | last post by:
Hi everybody, when migrating to the .NET 2.0 framework we decided to encourage the use of generic functions because they ought to run faster than their equivalent object-implemantations. Now...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: Vincent Finn | last post by:
Hi, I am trying to write a generic function and it isn't behaving as expected. I want to avoid having to write custom convert functions for my enums so I want to convert to an int and cast to...
5
by: guy.gorodish | last post by:
hi, i have a c# interop that pass struct into c dll. for some reason the data transfered into the c function is with incorrect order (meaning - members of the struct get other struct values and...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
12
by: pyramid | last post by:
I am new to C++, and one of the homework we have pertains to Functions. The problem is: Write a function called isValid that determines whether any 3 values can represent the sides of a...
0
by: sloan | last post by:
Is anyone else using the Microsoft Practices EnterpriseLibrary conventions for naming FILES (.cs) as far as a non-generic and generic implementation is concerned? Anyone have an alternative? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.