Correct way to convert an enum to value

convert string to enum java
enum to string
get enum from string c#
cannot convert enum to string c#
convert string to enum c++
enum valueof
c# get enum value
map string to enum c#

I work on a project that has a lot of enums defined like this:

enum BundleSize
{
    BUNDLE_SIZE_5 = 5,
    BUNDLE_SIZE_10 = 10,
    BUNDLE_SIZE_107 = 107,
};

I wrote a function to get the value from it like:

int convertBundleSizeEnumToVal(BundleSize b)
{
    switch(b)
    {
        case BUNDLE_SIZE_5: return 5; break;
        case BUNDLE_SIZE_10: return 10; break;
        case BUNDLE_SIZE_107: return 107; break;
        default: // handle appropriately and error out
    }
}

I realize I don't really need a converter (method 1 and 2 both work below):

BundleSize b = getRandomBundleSize();

printf("The size is %d\n", convertBundleSizeEnumToVal(b)); // method 1
printf("The size is %d\n", b); // method 2

But I feel that method 1 is still the "correct" way to do this, because in the future something like this might come along:

enum BundleSize
{
    BUNDLE_SIZE_5 = 5,
    BUNDLE_SIZE_5_POINT_5 = 6,
    BUNDLE_SIZE_10 = 10,
    BUNDLE_SIZE_107 = 107,
};

Which now effectively breaks method 2 without an obvious runtime error, whereas method 1 will atleast catch the problem in the default switch case.

I'm curious about what people think is the right way to handle these conversions.

You can use the static_cast operator:

static_cast<int>(b)

IMO, this is "correct". You shouldn't be defining functions to convert every possible enum variant to an integer, especially with large enums.

enum in Java, How do you find the int value of an enum? Enum.Parse. This converts strings to enum values. It is useful in programs that accept user input as a string, but store the value internally as an enum. An enum is a more efficient representation than a string.

Actually, you are using simple enum, so you can simply use your enum variable as a value.

For enum class (strict version of enum) you should use static_cast to convert your enum to value.

Get int value from enum in C#, then implement the constructor like you would any other class. The convertToDatabaseColumn () accept the enum as a parameter and calls its toString () method to convert the enum value to text. If the input is null, it isn’t converted. JPA uses this method when saving entities in a database. The convertToEntityAttribute () method is used for read operations.

You can simplify your function to:

int convertBundleSizeEnumToVal(BundleSize b)
{
    switch(b)
    {
        case BUNDLE_SIZE_5:
        case BUNDLE_SIZE_10:
        case BUNDLE_SIZE_107:
            return b;
        default: // handle appropriately and error out
    }
}

Method 2, is broken because the underlying type of enum is not necessarily int, and so the format specifier %d may be wrong and behaviour of the program may be undefined.

You can cast the enum value to int first, and it will be correct as long as you don't use higher values than can be represented by int. You can use either static_cast or explicit cast. Or you can use an implicit conversion:

int value = getRandomBundleSize();
printf("The size is %d\n", b);

Regarding which you should use, well it depends on what you need. If you think that enum values added later should not be converted by convertBundleSizeEnumToVal but should result in an error, then method 1 is indeed superior.

If you wish all enum values to be converted, then the function is error prone, as you would have to remember to add a case each time a new value is added. In this case method 2 is superior.

Java enum with multiple value types, In following example we will see both way of converting Enum into String in Java. This program also demonstrate use of Enum.values() method which returns all  I'm using your enum converter to populate a PropertyGrid field. the Description values do not get loaded into the resultant dropdown, in fact only the default value is populated in the dropdown, the rest is blank. my enum contains 5 values, only the first is available in the dropdown. any ideas? thanks in advance,ron. Ron

enum in C#, The naive way of doing this would be to write a giant switch statement returning the correct value of the enum for each of its possible values. Unscoped enum constants can be implicitly converted to int, but an int is never implicitly convertible to an enum value. The following example shows what happens if you try to assign hand a value that is not a Suit:

Enum.Parse Method (System), C# Enum To String. We can convert an enum to string by calling the ToString() method of an Enum. The following code example shows how to  Then conversions can be defined in OnModelCreating to store the enum values as strings (for example, "Donkey", "Mule", ) in the database: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .Entity<Rider>() .Property(e => e.Mount) .HasConversion( v => v.ToString(), v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v)); }

How to convert Enum to String in Java with Example, This example shows how to convert enum values to string and reversely. Enum to string. To convert enum to string use simply Enum.ToString method. [C#] Animal  Working with enums in Java 10 you have many options of creating and using enums. Which one to use depends on your needs: * create simple enum and get ordinal * create enum with code field (instance field) * access values by FOR loop * Iterate with EnumSet and forEach * Iterate enum with Stream * All examples and filter enum values More info on enums from Java docs: * Class Enum [https://docs

Comments
  • static_cast<int>(b).