Questions tagged [enums]

A data type consisting of a set of named values called elements, members or enumerators of the type.

This tag is for questions related to enumeration, enumerated-types (or enums) as related to programming.

In computer programming, an enumerated type (also called enumeration or enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value.

From the wikipedia article on enumerated type

Rust and Swift

Unlike the other languages that take inspiration from , the enums in and are sum types (see, for example, sum types in Haskell):

22254 questions
316
votes
7 answers

How to model type-safe enum types?

Scala doesn't have type-safe enums like Java has. Given a set of related constants, what would be the best way in Scala to represent those constants?
Jesper
  • 202,709
  • 46
  • 318
  • 350
304
votes
19 answers

Forward declaring an enum in C++

I'm trying to do something like the following: enum E; void Foo(E e); enum E {A, B, C}; which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why. Can anyone…
szevvy
  • 3,446
  • 2
  • 18
  • 10
287
votes
13 answers

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

I'm not sure what is the proper syntax for using C enums. I have the following code: enum {RANDOM, IMMEDIATE, SEARCH} strategy; strategy = IMMEDIATE; But this does not compile, with the following error: error: conflicting types for…
lindelof
  • 34,556
  • 31
  • 99
  • 140
286
votes
6 answers

How to cast int to enum in C++?

How do I cast an int to an enum in C++? For example: enum Test { A, B }; int a = 1; How do I convert a to type Test::A?
user1509260
  • 3,353
  • 3
  • 18
  • 9
284
votes
8 answers

How to convert enum value to int?

I have a function which return a type int. However, I only have a value of the TAX enumeration. How can I cast the TAX enumeration value to an int? public enum TAX { NOTAX(0),SALESTAX(10),IMPORTEDTAX(5); private int value; private…
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
283
votes
19 answers

Jackson enum Serializing and DeSerializer

I'm using JAVA 1.6 and Jackson 1.9.9 I've got an enum public enum Event { FORGOT_PASSWORD("forgot password"); private final String value; private Event(final String description) { this.value = description; } …
pookieman
  • 2,981
  • 2
  • 14
  • 8
256
votes
32 answers

Java: Check if enum contains a given string?

Here's my problem - I'm looking for (if it even exists) the enum equivalent of ArrayList.contains();. Here's a sample of my code problem: enum choices {a1, a2, b1, b2}; if(choices.???(a1)}{ //do this } Now, I realize that an ArrayList of Strings…
Jared
  • 4,823
  • 6
  • 23
  • 15
250
votes
6 answers

Get Enum from Description attribute

Possible Duplicate: Finding an enum value by its Description Attribute I have a generic extension method which gets the Description attribute from an Enum: enum Animal { [Description("")] NotSet = 0, [Description("Giant Panda")] …
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
249
votes
15 answers

How to use enums in C++

Suppose we have an enum like the following: enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday}; I want to create an instance of this enum and initialize it with a proper value, so I do: Days day = Days.Saturday; Now I want to check…
Hossein
  • 24,202
  • 35
  • 119
  • 224
241
votes
9 answers

Is there a way to check if int is legal enum in C#?

I've read a few SO posts and it seems most basic operation is missing. public enum LoggingLevel { Off = 0, Error = 1, Warning = 2, Info = 3, Debug = 4, Trace = 5 }; if (s == "LogLevel") { _log.LogLevel =…
char m
  • 7,840
  • 14
  • 68
  • 117
241
votes
5 answers

Getting value of enum on string conversion

I have the following enum defined: from enum import Enum class D(Enum): x = 1 y = 2 print(D.x) now the printed value is D.x instead, I wanted the enum's value to be print 1 What can be done to achieve this functionality?
Vaibhav Mishra
  • 11,384
  • 12
  • 45
  • 58
241
votes
30 answers

How to use enums as flags in C++?

Treating enums as flags works nicely in C# via the [Flags] attribute, but what's the best way to do this in C++? For example, I'd like to write: enum AnimalFlags { HasClaws = 1, CanFly =2, EatsFish = 4, Endangered =…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
238
votes
31 answers

TypeScript enum to object array

I have an enum defined this way: export enum GoalProgressMeasurements { Percentage = 1, Numeric_Target = 2, Completed_Tasks = 3, Average_Milestone_Progress = 4, Not_Measured = 5 } However, I'd like it to be represented as an…
AnimaSola
  • 7,146
  • 14
  • 43
  • 62
238
votes
2 answers

Naming of enums in Java: Singular or Plural?

Is there an "official" recommendation of how to name Java enums? enum Protocol { HTTP, HTTPS, FTP } or enum Protocols { HTTP, HTTPS, FTP } I know in the .Net world the recommendation is to use singular except for enums that represent bit flags.…
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
235
votes
9 answers

How to set enum to null

I have an enum string name; public enum Color { Red, Green, Yellow } How to set it to NULL on load. name = ""; Color color = null; //error Edited: My bad, I didn't explain it properly. But all the answers related to nullable is perfect. My…
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112