Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get enum value by keyname

Tags:

c#

public enum aa{ a1=1,a2=2,a3=6,...,a100=203}

How to get value like this

string att=GetFromDatabase("attribute");    //this return a1 or a2 ...
Enum.GetValue(att);
like image 532
ebattulga Avatar asked Feb 12 '09 10:02

ebattulga


People also ask

How do you find the value of an enum?

Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

Can you index enums?

Yes you can programmatically index an enum, text, or menu ring.

How are enums numbered?

Enum ValuesIf values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default. The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member.

What is enum TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.


2 Answers

Something like this should do the trick:

aa attEnum = (aa)Enum.Parse(typeof(aa), att);

Go to http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx for more details.

like image 121
samjudson Avatar answered Sep 22 '22 14:09

samjudson


Solution

string name = GetFromDatabase("attribute");
Enum.Parse(typeof(aa),name);
like image 45
Bartek Szabat Avatar answered Sep 20 '22 14:09

Bartek Szabat