Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumeration and comparison 2

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
NZ
Hi guys

I have a set of defined values which a user can pass to my program. I have set up a type enum as follows:

private enum PayMethod
{
CC = 1,
DB = 2,
CH = 3,
DD = 4,
SD = 5
}

What I want to do is ensure that the passed value is one of these options - either the character part or the numeric value. The value is passed in an XML node.

I've tried to create a function to compare the values but I don't want to do multiple if/case statements. Is there any way I can do a direct comparison between my values in the enum and the passed value?

Thanks as always

Craftor
:cool:

 
You can use
Enum.IsDefined(typeof(PayMethod), constant_to_test);

 
Thanks alexboly - that's precisely the kind of thing I was looking for!

In case anyone else is interested, I added the following to alexboly's code to test if the user has passed a valid numeric representation of the enum:

if (!Enum.IsDefined(typeof(PayMethod), Enum.Parse(typeof(PayMethod), paymethod)))
{
return false;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top