How to use enums in C++

This code is wrong: enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday}; Days day = Days.Saturday; if (day == Days.Saturday) Because Days is not a scope, nor object. It is a type. And Types themselves don’t have members. What you wrote is the equivalent to std::string.clear. std::string is a type, so you can’t use . … Read more

Get Enum from Description attribute [duplicate]

public static class EnumEx { public static T GetValueFromDescription<T>(string description) where T : Enum { foreach(var field in typeof(T).GetFields()) { if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute) { if (attribute.Description == description) return (T)field.GetValue(null); } else { if (field.Name == description) return (T)field.GetValue(null); } } throw new ArgumentException(“Not found.”, nameof(description)); // Or return default(T); } } … Read more

Jackson enum Serializing and DeSerializer

The serializer / deserializer solution pointed out by @xbakesx is an excellent one if you wish to completely decouple your enum class from its JSON representation. Alternatively, if you prefer a self-contained solution, an implementation based on @JsonCreator and @JsonValue annotations would be more convenient. So leveraging on the example by @Stanley the following is … Read more

How to convert enum value to int?

You’d need to make the enum expose value somehow, e.g. public enum Tax { NONE(0), SALES(10), IMPORT(5); private final int value; private Tax(int value) { this.value = value; } public int getValue() { return value; } } … public int getTaxValue() { Tax tax = Tax.NONE; // Or whatever return tax.getValue(); } (I’ve changed the … Read more

How to define an enumerated type (enum) in C?

It’s worth pointing out that you don’t need a typedef. You can just do it like the following enum strategy { RANDOM, IMMEDIATE, SEARCH }; enum strategy my_strategy = IMMEDIATE; It’s a style question whether you prefer typedef. Without it, if you want to refer to the enumeration type, you need to use enum strategy. … Read more

Forward declaring an enum in C++

Forward declaration of enums is possible since C++11. Previously, the reason enum types couldn’t be forward declared was because the size of the enumeration depended on its contents. As long as the size of the enumeration is specified by the application, it can be forward declared: enum Enum1; // Illegal in C++03 and C++11; no … Read more

What is the default value for enum variable?

It is whatever member of the enumeration represents the value 0. Specifically, from the documentation: The default value of an enum E is the value produced by the expression (E)0. As an example, take the following enum: enum E { Foo, Bar, Baz, Quux } Without overriding the default values, printing default(E) returns Foo since … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)