How to Get Enum Item Name from Its Value

Enum String Name from Value

You can convert the int back to an enumeration member with a simple cast, and then call ToString():

int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();

get enum name from enum value [duplicate]

Since your 'value' also happens to match with ordinals you could just do:

public enum RelationActiveEnum {
Invited,
Active,
Suspended;

private final int value;

private RelationActiveEnum() {
this.value = ordinal();
}
}

And getting a enum from the value:

int value = 1;
RelationActiveEnum enumInstance = RelationActiveEnum.values()[value];

I guess an static method would be a good place to put this:

public enum RelationActiveEnum {
public static RelationActiveEnum fromValue(int value)
throws IllegalArgumentException {
try {
return RelationActiveEnum.values()[value]
} catch(ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Unknown enum value :"+ value);
}
}
}

Obviously this all falls apart if your 'value' isn't the same value as the enum ordinal.

How to get names of enum entries?

The code you posted will work; it will print out all the members of the enum, including the values of the enum members. For example, the following code:

enum myEnum { bar, foo }

for (var enumMember in myEnum) {
console.log("enum member: ", enumMember);
}

Will print the following:

Enum member: 0
Enum member: 1
Enum member: bar
Enum member: foo

If you instead want only the member names, and not the values, you could do something like this:

for (var enumMember in myEnum) {
var isValueProperty = Number(enumMember) >= 0
if (isValueProperty) {
console.log("enum member: ", myEnum[enumMember]);
}
}

That will print out just the names:

Enum member: bar  
Enum member: foo

Caveat: this slightly relies on an implementation detail: TypeScript compiles enums to a JS object with the enum values being members of the object. If TS decided to implement them different in the future, the above technique could break.

How to get an enum value from a string value in Java

Yes, Blah.valueOf("A") will give you Blah.A.

Note that the name must be an exact match, including case: Blah.valueOf("a") and Blah.valueOf("A ") both throw an IllegalArgumentException.

The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods.

Get value of enum member by its name?

Assuming that KeyVal is a string representing the name of a certain enum you could do this in the following way:

int value = (int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal);

Get the name of Enum value

Given an enum

public enum Week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

here are the things you can do:

static void Main(string[] args)
{

// enum to int
int i=(int)Week.Thursday;

// int to enum;
Week day=(Week)3;

// enum to string
string name=Week.Thursday.ToString();
string fun=Enum.GetName(typeof(Week), 6);
string agh=Enum.GetName(typeof(Week), Week.Monday);
string wed=EnumName(Week.Wednesday);

// string to enum
Week apt=(Week)Enum.Parse(typeof(Week), "Thursday");

// all values of an enum type
Week[] days=(Week[])Enum.GetValues(typeof(Week));

// all names of an enum type
string[] names=Enum.GetNames(typeof(Week));

}

static string EnumName<T>(T value)
{
return Enum.GetName(typeof(T), value);
}

Edit 1

If you want to convert from one enum to another enum of different type based on the underlying numeric value (convert to integer and from integer), then use the following:

/// <summary>
/// Casts one enum type to another based on the underlying value
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="otherEnum">The other enum.</param>
public static TEnum CastTo<TEnum>(this Enum otherEnum)
{
return (TEnum)Enum.ToObject(typeof(TEnum), Convert.ToInt32(otherEnum));
}

to be used as

public enum WeekEnd
{
Saturday = Week.Saturday,
Sunday = Week.Sunday
}

static void Main(string[] args)
{
var day = WeekEnd.Saturday.CastTo<Week>();
// Week.Sunday
}

Get enum name when value is known

return ((MyEnumClass)n).ToString();


Related Topics



Leave a reply



Submit