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
171
votes
9 answers

Cannot approach Typescript enum within HTML

I made an enum with Typescript to use in MyService.service.ts MyComponent.component.ts and MyComponent.component.html. export enum ConnectionResult { Success, Failed } I can easily get and compare a defined enum variable from…
Klyner
  • 3,983
  • 4
  • 26
  • 50
169
votes
6 answers

How to Create and Use Enum in Mongoose

I am trying to create and use an enum type in Mongoose. I checked it out, but I'm not getting the proper result. I'm using enum in my program as follows: My schema is: var RequirementSchema = new mongooseSchema({ status: { type: String, …
Prabjot Singh
  • 4,491
  • 8
  • 31
  • 51
166
votes
7 answers

What is the size of an enum in C?

I'm creating a set of enum values, but I need each enum value to be 64 bits wide. If I recall correctly, an enum is generally the same size as an int; but I thought I read somewhere that (at least in GCC) the compiler can make the enum any width…
mipadi
  • 398,885
  • 90
  • 523
  • 479
165
votes
7 answers

Why do enum permissions often have 0, 1, 2, 4 values?

Why are people always using enum values like 0, 1, 2, 4, 8 and not 0, 1, 2, 3, 4? Has this something to do with bit operations, etc.? I would really appreciate a small sample snippet on how this is used correctly :) [Flags] public enum…
Pascal
  • 12,265
  • 25
  • 103
  • 195
163
votes
15 answers

How do I test if int value exists in Python Enum without using try/catch?

Using the Python Enum class, is there a way to test if an Enum contains a specific int value without using try/catch? With the following class: from enum import Enum class Fruit(Enum): Apple = 4 Orange = 5 Pear = 6 How can I test for…
Nathan Kovner
  • 2,513
  • 3
  • 18
  • 14
163
votes
5 answers

How to convert int to Enum in python?

Using the new Enum feature (via backport enum34) with python 2.7.6. Given the following definition, how can I convert an int to the corresponding Enum value? from enum import Enum class Fruit(Enum): Apple = 4 Orange = 5 Pear = 6 I know…
User
  • 62,498
  • 72
  • 186
  • 247
162
votes
19 answers

How to iterate over values of an Enum having flags?

If I have a variable holding a flags enum, can I somehow iterate over the single-bit values in that specific variable? Or do I have to use Enum.GetValues to iterate over the entire enum and check which ones are set?
user502255
162
votes
11 answers

How to Compare Flags in C#?

I have a flag enum below. [Flags] public enum FlagTest { None = 0x0, Flag1 = 0x1, Flag2 = 0x2, Flag3 = 0x4 } I cannot make the if statement evaluate to true. FlagTest testItem = FlagTest.Flag1 | FlagTest.Flag2; if (testItem ==…
David Basarab
  • 72,212
  • 42
  • 129
  • 156
162
votes
6 answers

Enum type constraints in C#

Possible Duplicate: Anyone know a good workaround for the lack of an enum generic constraint? What is the reason behind C# not allowing type constraints on Enum's? I'm sure there is a method behind the madness, but I'd like to understand why it's…
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
158
votes
7 answers

How to create enum like type in TypeScript?

I'm working on a definitions file for the Google maps API for TypeScript. And I need to define an enum like type eg. google.maps.Animation which contains two properties: BOUNCE and DROP. How should this be done in TypeScript?
eNepper
  • 1,889
  • 2
  • 13
  • 12
156
votes
13 answers

Jackson databind enum case insensitive

How can I deserialize JSON string that contains enum values that are case insensitive? (using Jackson Databind) The JSON string: [{"url": "foo", "type": "json"}] and my Java POJO: public static class Endpoint { public enum DataType { …
tom91136
  • 8,662
  • 12
  • 58
  • 74
154
votes
20 answers

How to define an enum with string value?

I am trying to define an Enum and add valid common separators which used in CSV or similar files. Then I am going to bind it to a ComboBox as a data source so whenever I add or remove from the Enum definition, I would not need to change anything in…
Dumbo
  • 13,555
  • 54
  • 184
  • 288
154
votes
17 answers

Should an Enum start with a 0 or a 1?

Imagine I have defined the following Enum: public enum Status : byte { Inactive = 1, Active = 2, } What's the best practice to use enum? Should it start with 1 like the above example or start with 0 (without the explicit values) like…
Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
153
votes
5 answers

Is it possible to assign numeric value to an enum in Java?

Is anything like this possible in Java? Can one assign custom numeric values to enum elements in Java? public enum EXIT_CODE { A=104, B=203; }
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
153
votes
4 answers

What's the common practice for enums in Python?

How can I implement an enumeration type (spelled enum in some languages) in Python? What is the common practice to get this functionality?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689