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();
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.
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 string value from Enum
You just need to call .ToString
t.Status = Status.Success.ToString();
ToString() on Enum from MSDN
If you have enum Id passed, you can run:
t.Status = ((Status)enumId).ToString();
It casts integer to Enum value and then call ToString()
EDIT (better way):
You can even change your method to:
public void CreateStatus(Status status , string userName)
and call it:
CreateStatus(1,"whatever");
and cast to string:
t.Status = status.ToString();
Find enum value by enum name in string - Python
As per the manual:
>>> Animal.DOG.value
'doggy'
>>> Animal.DOG.name
'DOG'
>>> # For more programmatic access, with just the enum member name as a string:
>>> Animal['DOG'].value
'doggy'
get enum name from enum value
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.
Get enum value by name stored in a string in PHP
Well, it seems there is not any built-in solution in PHP. I've solve this with a custom function:
enum Status : int
{
case ACTIVE = 1;
case REVIEWED = 2;
// ...
public static function fromName(string $name): string
{
foreach (self::cases() as $status) {
if( $name === $status->name ){
return $status->value;
}
}
throw new \ValueError("$name is not a valid backing value for enum " . self::class );
}
}
Then, I simply use Status::fromName('ACTIVE')
and get 1
If you want to mimic the from
and tryFrom
enum functions, you can also add:
public static function tryFromName(string $name): string|null
{
try {
return self::fromName($name);
} catch (\ValueError $error) {
return null;
}
}
Related Topics
Unzip Files Programmatically in .Net
How to Get the Computer Name in .Net
Value of Type 'T' Cannot Be Converted To
Ef Including Other Entities (Generic Repository Pattern)
How to Programmatically Fill in a Form and 'Post' a Web Page
How to Get a List of Properties with a Given Attribute
How to Edit CSS Style of a Div Using C# in .Net
Large Object Heap Fragmentation
Why We Have Both Jagged Array and Multidimensional Array
Hide Tabcontrol Buttons to Manage Stacked Panel Controls