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
212
votes
10 answers

How to delete an enum type value in postgres?

How do I delete an enum type value that I created in postgresql? create type admin_level1 as enum('classifier', 'moderator', 'god'); E.g. I want to remove moderator from the list. I can't seem to find anything on the docs. I'm using Postgresql…
Amjith
  • 22,626
  • 14
  • 43
  • 38
211
votes
18 answers

Enums in Javascript with ES6

I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS. The best I can come up with is: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN:…
cypherfunc
  • 2,251
  • 2
  • 12
  • 14
209
votes
15 answers

Convert Enum to String

Which is the preferred way to convert an Enum to a String in .NET 3.5? Enum.GetName Enum.Format ToString Why should I prefer one of these over the others? Does one perform better?
Eric Weilnau
  • 5,370
  • 4
  • 30
  • 30
209
votes
5 answers

SQL query to get all values a enum can have

Postgresql got enum support some time ago. CREATE TYPE myenum AS ENUM ( 'value1', 'value2', ); How do I get all values specified in the enum with a query?
Wienczny
  • 3,958
  • 4
  • 30
  • 35
206
votes
13 answers

C# vs Java Enum (for those new to C#)

I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#. I'm trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#'s enums seem to be…
Ogre Psalm33
  • 21,366
  • 16
  • 74
  • 92
206
votes
32 answers

Dart How to get the name of an enum as a String

Before enums were available in Dart I wrote some cumbersome and hard to maintain code to simulate enums and now want to simplify it. I need to get the name of the enum as a string such as can be done with Java but cannot. For instance little test…
Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34
205
votes
13 answers

Can my enums have friendly names?

I have the following enum public enum myEnum { ThisNameWorks, This Name doesn't work Neither.does.this; } Is it not possible to have enums with "friendly names"?
JL.
  • 78,954
  • 126
  • 311
  • 459
204
votes
7 answers

Java enum - why use toString instead of name

If you look in the enum api at the method name() it says that: Returns the name of this enum constant, exactly as declared in its enum declaration. Most programmers should use the toString method in preference to this one, as…
spauny
  • 4,976
  • 9
  • 44
  • 61
203
votes
9 answers

Can I set enum start value in Java?

I use the enum to make a few constants: enum ids {OPEN, CLOSE}; the OPEN value is zero, but I want it as 100. Is it possible?
qrtt1
  • 7,746
  • 8
  • 42
  • 62
198
votes
20 answers

Swift: Convert enum value to String?

Given the following enum: enum Audience { case Public case Friends case Private } How do I get the string "Public" from the audience constant below? let audience = Audience.Public
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
198
votes
3 answers

What is the difference between `Enum.name()` and `Enum.toString()`?

After reading the documentation of String java.lang.Enum.name() I am not sure I understand when to use name() and when to use toString(). Returns the name of this enum constant, exactly as declared in its enum declaration. Most programmers should…
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
198
votes
12 answers

How can I lookup a Java enum from its String value?

I would like to lookup an enum from its string value (or possibly any other value). I've tried the following code but it doesn't allow static in initialisers. Is there a simple way? public enum Verbosity { BRIEF, NORMAL, FULL; private…
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
197
votes
15 answers

Should enums in C# have their own file?

I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful. What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its…
Finglas
  • 15,518
  • 10
  • 56
  • 89
197
votes
3 answers

Where is the documentation for the values() method of Enum?

I declare an enum as : enum Sex {MALE,FEMALE}; And then, iterate enum as shown below : for(Sex v : Sex.values()){ System.out.println(" values :"+ v); } I checked the Java API but can't find the values() method? I'm curious as to where this…
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
196
votes
5 answers

Typescript has unions, so are enums redundant?

Ever since TypeScript introduced unions types, I wonder if there is any reason to declare an enum type. Consider the following enum type declaration: enum X { A, B, C } var x: X = X.A; and a similar union type declaration: type X: "A" | "B" |…
prmph
  • 7,616
  • 11
  • 37
  • 46