Java Enum return Int

Font.PLAIN is not an enum. It is just an int. If you need to take the value out of an enum, you can’t avoid calling a method or using a .value, because enums are actually objects of its own type, not primitives. If you truly only need an int, and you are already to accept … Read more

C++ string to enum

A std::map<std::string, MyEnum> (or unordered_map) could do it easily. Populating the map would be just as tedious as the switch statement though. Edit: Since C++11, populating is trivial:

Get int value from enum in C#

Just cast the enum, e.g. The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, or ulong, it should be … Read more

Using Enum values as String literals

You can’t. I think you have FOUR options here. All four offer a solution but with a slightly different approach… Option One: use the built-in name() on an enum. This is perfectly fine if you don’t need any special naming format. Option Two: add overriding properties to your enums if you want more control Option Three: use static finals instead … Read more

Why Python 3.6.1 throws AttributeError: module ‘enum’ has no attribute ‘IntFlag’?

It’s because your enum is not the standard library enum module. You probably have the package enum34 installed. One way check if this is the case is to inspect the property enum.__file__ Since python 3.6 the enum34 library is no longer compatible with the standard library. The library is also unnecessary, so you can simply uninstall it. If you need the code … Read more

Convert Enum to String

As of C#6 the best way to get the name of an enum is the new nameof operator: This works at compile time, with the enum being replaced by the string in the compiled result, which in turn means this is the fastest way possible. Any use of enum names does interfere with code obfuscation, if you … Read more

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.