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
232
votes
14 answers

Choosing the default value of an Enum type without having to change values

In C#, is it possible to decorate an Enum type with an attribute or do something else to specify what the default value should be, without having the change the values? The numbers required might be set in stone for whatever reason, and it'd be…
xyz
  • 27,223
  • 29
  • 105
  • 125
232
votes
10 answers

How do I make an enum Decodable in Swift?

enum PostType: Decodable { init(from decoder: Decoder) throws { // What do i put here? } case Image enum CodingKeys: String, CodingKey { case image } } What do i put to complete this? Also, lets say i changed…
swift nub
  • 2,747
  • 3
  • 17
  • 39
232
votes
8 answers

Can a C++ enum class have methods?

I have an enum class with two values, and I want to create a method which receives a value and returns the other one. I also want to maintain type safety(that's why I use enum class instead of…
bsky
  • 19,326
  • 49
  • 155
  • 270
230
votes
8 answers

How to remove an item for a OR'd enum?

I have an enum like: public enum Blah { RED = 2, BLUE = 4, GREEN = 8, YELLOW = 16 } Blah colors = Blah.RED | Blah.BLUE | Blah.YELLOW; How could I remove the color blue from the variable colors?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
228
votes
18 answers

How to check if any flags of a flag combination are set?

Let's say I have this enum: [Flags] enum Letters { A = 1, B = 2, C = 4, AB = A | B, All = A | B | C, } To check if for example AB is set I can do this: if((letter & Letters.AB) == Letters.AB) Is there a simpler way to…
Svish
  • 152,914
  • 173
  • 462
  • 620
227
votes
16 answers

Why would an Enum implement an Interface?

I just found out that Java allows enums to implement an interface. What would be a good use case for that?
unj2
  • 52,135
  • 87
  • 247
  • 375
224
votes
6 answers

Implementing Singleton with an Enum (in Java)

I have read that it is possible to implement Singleton in Java using an Enum such as: public enum MySingleton { INSTANCE; } But, how does the above work? Specifically, an Object has to be instantiated. Here, how is MySingleton being…
Anand
  • 20,708
  • 48
  • 131
  • 198
222
votes
26 answers

How do I get the count of a Swift enum?

How can I determine the number of cases in a Swift enum? (I would like to avoid manually enumerating through all the values, or using the old "enum_count trick" if possible.)
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
222
votes
8 answers

How do I add more members to my ENUM-type column in MySQL?

The MySQL reference manual does not provide a clearcut example on how to do this. I have an ENUM-type column of country names that I need to add more countries to. What is the correct MySQL syntax to achieve this? Here's my attempt: ALTER TABLE…
Zaid
  • 36,680
  • 16
  • 86
  • 155
219
votes
9 answers

Why do you use typedef when declaring an enum in C++?

I haven't written any C++ in years and now I'm trying to get back into it. I then ran across this and thought about giving up: typedef enum TokenType { blah1 = 0x00000000, blah2 = 0X01000000, blah3 = 0X02000000 } TokenType; What…
Tim Merrifield
  • 6,088
  • 5
  • 31
  • 33
219
votes
8 answers

Map enum in JPA with fixed values?

I'm looking for the different ways to map an enum using JPA. I especially want to set the integer value of each enum entry and to save only the integer value. @Entity @Table(name = "AUTHORITY_") public class Authority implements Serializable { …
Kartoch
  • 7,610
  • 9
  • 40
  • 68
218
votes
11 answers

Most common C# bitwise operations on enums

For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have. For example: flags = flags | FlagsEnum.Bit4; …
steffenj
  • 7,967
  • 10
  • 35
  • 41
217
votes
6 answers

Methods inside enum in C#

In Java, it's possible to have methods inside an enum. Is there such possibility in C# or is it just a string collection and that's it? I tried to override ToString() but it does not compile. Does someone have a simple code sample?
Eya
  • 2,183
  • 2
  • 12
  • 5
217
votes
16 answers

Pick a random value from an enum?

If I have an enum like this: public enum Letter { A, B, C, //... } What is the best way to pick one randomly? It doesn't need to be production quality bulletproof, but a fairly even distribution would be nice. I could do something…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
216
votes
11 answers

How do I select a random value from an enumeration?

Given an arbitrary enumeration in C#, how do I select a random value? (I did not find this very basic question on SO. I'll post my answer in a minute as reference for anyone, but please feel free to post your own answer.)
mafu
  • 31,798
  • 42
  • 154
  • 247