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
422
votes
8 answers

Best way to create enum of strings?

What is the best way to have a enum type represent a set of strings? I tried this: enum Strings{ STRING_ONE("ONE"), STRING_TWO("TWO") } How can I then use them as Strings?
Dori
  • 18,283
  • 17
  • 74
  • 116
412
votes
28 answers

How can I iterate over an enum?

I just noticed that you can not use standard math operators on an enum such as ++ or +=. So what is the best way to iterate through all of the values in a C++ enum?
Adam
  • 25,966
  • 23
  • 76
  • 87
407
votes
1 answer

How can I loop through enum values for display in radio buttons?

What is the proper way to loop through literals of an enum in TypeScript? (I am currently using TypeScript 1.8.1.) I've got the following enum: export enum MotifIntervention { Intrusion, Identification, AbsenceTest, Autre } export…
Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58
399
votes
8 answers

Java: using switch statement with enum under subclass

First I'll state that I'm much more familiar with enums in C# and it seems like enums in java is a quite mess. As you can see, I'm trying to use a switch statement @ enums in my next example but I always get an error no matter what I'm doing. The…
Popokoko
  • 6,413
  • 16
  • 47
  • 58
397
votes
15 answers

Android: How to put an Enum in a Bundle?

How do you add an Enum object to an Android Bundle?
zer0stimulus
  • 22,306
  • 30
  • 110
  • 141
384
votes
17 answers

Cast Int to enum in Java

What is the correct way to cast an Int to an enum in Java given the following enum? public enum MyEnum { EnumValue1, EnumValue2 } MyEnum enumValue = (MyEnum) x; //Doesn't work???
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
381
votes
17 answers

How to get all values from python enum class?

I'm using Enum4 library to create an enum class as follows: class Color(Enum): RED = 1 BLUE = 2 I want to print [1, 2] as a list somewhere. How can I achieve this?
user1159517
  • 5,390
  • 8
  • 30
  • 47
381
votes
19 answers

Adding a new value to an existing ENUM Type

I have a table column that uses an enum type. I wish to update that enum type to have an additional possible value. I don't want to delete any existing values, just add the new value. What is the simplest way to do this?
Ian
  • 24,116
  • 22
  • 58
  • 96
369
votes
25 answers

How to implement Enums in Ruby?

What's the best way to implement the enum idiom in Ruby? I'm looking for something which I can use (almost) like the Java/C# enums.
auramo
  • 13,167
  • 13
  • 66
  • 88
365
votes
15 answers

Convert from enum ordinal to enum type

I've the enum type ReportTypeEnum that get passed between methods in all my classes but I then need to pass this on the URL so I use the ordinal method to get the int value. After I get it in my other JSP page, I need to convert it to back to an…
Lennie
  • 10,605
  • 5
  • 22
  • 13
365
votes
26 answers

Enum ToString with user friendly strings

My enum consists of the following values: private enum PublishStatusses{ NotCompleted, Completed, Error }; I want to be able to output these values in a user friendly way though. I don't need to be able to go from string to value again.…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
350
votes
10 answers

Enum Naming Convention - Plural

I'm asking this question despite having read similar but not exactly what I want at C# naming convention for enum and matching property I found I have a tendency to name enums in plural and then 'use' them as singular, example: public enum…
o.k.w
  • 25,490
  • 6
  • 66
  • 63
335
votes
3 answers

What is the default value for enum variable?

An enum variable, anyone know if it is always defaulting to the first element?
user496949
  • 83,087
  • 147
  • 309
  • 426
331
votes
9 answers

Convert string to Enum in Python

What's the correct way to convert a string to a corresponding instance of an Enum subclass? Seems like getattr(YourEnumType, str) does the job, but I'm not sure if it's safe enough. As an example, suppose I have an enum like class BuildType(Enum): …
Vladius
  • 4,268
  • 2
  • 20
  • 21
328
votes
7 answers

Coding Conventions - Naming Enums

Is there a convention for naming enumerations in Java? My preference is that an enum is a type. So, for instance, you have an enum Fruit{Apple,Orange,Banana,Pear, ... } NetworkConnectionType{LAN,Data_3g,Data_4g, ... } I am opposed to naming…
Walter White