Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Oct 28, 2008 02:53 PM by Mr^B
Member
74 Points
158 Posts
Jun 04, 2007 05:53 PM|LINK
I have this enum
{
Operations = 1001,
Personnel = 1002,
Safety = 1003
}
I then define:
CategoriesType mycategory = CategoriesType.Operations;
How can I get 1001 that i have define in my enum, so far I only figure how to get Operations but not the actual value
Contributor
5816 Points
782 Posts
Jun 04, 2007 06:07 PM|LINK
int x = (int) Enum.Parse(typeof (CategoriesType), Enum.GetName(typeof (CategoriesType), mycategory));
491 Points
98 Posts
Jun 04, 2007 06:40 PM|LINK
you have to convert value into integer.
TRY THIS CODE:
int value = (int) Enum.Parse(typeof (CategoriesType), Enum.GetName(typeof (CategoriesType), mycategory));
Jun 04, 2007 06:44 PM|LINK
suresh modiya you have to convert value into integer. TRY THIS CODE: int value = (int) Enum.Parse(typeof (CategoriesType), Enum.GetName(typeof (CategoriesType), mycategory));
You cannot use 'value' as it's one of the protected keyword in c# and vb.net
304 Points
53 Posts
Jun 04, 2007 07:04 PM|LINK
int x = (int)CategoriesType.Operations;
Jun 04, 2007 07:37 PM|LINK
thank you everyone, I tried everyone samples and it seems that this does the job the simplest
int x= (int)Category;
Jun 06, 2007 02:34 PM|LINK
int categoryvalue = (int) Enum.Parse(typeof (CategoriesType), Enum.GetName(typeof (CategoriesType), mycategory));
76 Points
24 Posts
Oct 28, 2008 02:42 PM|LINK
Awesome. This returns the value of Enum. Thanks jackyang.
Star
12726 Points
2245 Posts
Oct 28, 2008 02:53 PM|LINK
lol @ the convoluted way of casting an enum to an int, this is ALL you need:
(int) mycategory;
Where you would use Enum.Parse is if you wanted to determine an Enum representation from say a string, eg:
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
Is taking an enum, finding its name, then parsing the name to find the enum AGAIN, and then (finally) casting the original enum to an int.
JohnStern
Member
74 Points
158 Posts
How can I get a enum value
Jun 04, 2007 05:53 PM|LINK
I have this enum
public enum CategoriesType : int{
Operations = 1001,
Personnel = 1002,
Safety = 1003
}
I then define:
CategoriesType mycategory = CategoriesType.Operations;
How can I get 1001 that i have define in my enum, so far I only figure how to get Operations but not the actual value
jackyang
Contributor
5816 Points
782 Posts
Re: How can I get a enum value
Jun 04, 2007 06:07 PM|LINK
.NET Developer
ASP.NET/jQuery Spell Checker
suresh modiy...
Member
491 Points
98 Posts
Re: How can I get a enum value
Jun 04, 2007 06:40 PM|LINK
Thanks.....
jackyang
Contributor
5816 Points
782 Posts
Re: How can I get a enum value
Jun 04, 2007 06:44 PM|LINK
You cannot use 'value' as it's one of the protected keyword in c# and vb.net
.NET Developer
ASP.NET/jQuery Spell Checker
akamal
Member
304 Points
53 Posts
Re: How can I get a enum value
Jun 04, 2007 07:04 PM|LINK
Senior .Net Developer
amirkamal.com
JohnStern
Member
74 Points
158 Posts
Re: How can I get a enum value
Jun 04, 2007 07:37 PM|LINK
thank you everyone, I tried everyone samples and it seems that this does the job the simplest
int x= (int)Category;
suresh modiy...
Member
491 Points
98 Posts
Re: How can I get a enum value
Jun 06, 2007 02:34 PM|LINK
Thanks.....
Zenthil
Member
76 Points
24 Posts
Re: How can I get a enum value
Oct 28, 2008 02:42 PM|LINK
Awesome. This returns the value of Enum. Thanks jackyang.
int x = (int) Enum.Parse(typeof (CategoriesType), Enum.GetName(typeof (CategoriesType), mycategory));
Mr^B
Star
12726 Points
2245 Posts
Re: How can I get a enum value
Oct 28, 2008 02:53 PM|LINK
lol @ the convoluted way of casting an enum to an int, this is ALL you need:
(int) mycategory;
Where you would use Enum.Parse is if you wanted to determine an Enum representation from say a string, eg:
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");Whereas...Is taking an enum, finding its name, then parsing the name to find the enum AGAIN, and then (finally) casting the original enum to an int.