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):

19721 questions
4012
votes
32 answers

How to enumerate an enum

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { …
Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
3463
votes
32 answers

How can I cast int to enum?

How can an int be cast to an enum in C#?
lomaxx
  • 104,787
  • 56
  • 140
  • 177
2259
votes
51 answers

What is the preferred syntax for defining enums in JavaScript?

What is the preferred syntax for defining enums in JavaScript? Something like: my.namespace.ColorEnum = { RED : 0, GREEN : 1, BLUE : 2 } // later on if(currentColor == my.namespace.ColorEnum.RED) { // whatever } Or is there a more…
David Citron
  • 41,009
  • 18
  • 60
  • 71
2128
votes
29 answers

How to get an enum value from a string value in Java?

Say I have an enum which is just public enum Blah { A, B, C, D } and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to do this? Is the Enum.valueOf() the method I need? If so,…
Malachi
  • 30,486
  • 16
  • 60
  • 96
2028
votes
29 answers

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this. public enum Question { Role = 2, ProjectFunding = 3, TotalEmployee = 4, NumberOfServers = 5, …
jim
  • 23,608
  • 13
  • 48
  • 66
1875
votes
14 answers

Comparing Java enum members: == or equals()?

I know that Java enums are compiled to classes with private constructors and a bunch of public static members. When comparing two members of a given enum, I've always used .equals(), e.g. public useEnums(SomeEnum a) { …
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
1586
votes
8 answers

How to loop through all enum values in C#?

This question already has an answer here: How do I enumerate an enum in C#? 26 answers public enum Foos { A, B, C } Is there a way to loop through the possible values of Foos? Basically? foreach(Foo in Foos)
divinci
  • 20,239
  • 11
  • 42
  • 56
1545
votes
13 answers

What does the [Flags] Enum Attribute mean in C#?

From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what exactly the [Flags] attribute does. Anyone have a good…
Brian Leahy
  • 32,433
  • 10
  • 42
  • 60
1276
votes
21 answers

Create Generic method constraining T to an Enum

I'm building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString(string value, T…
johnc
  • 36,657
  • 37
  • 96
  • 137
1243
votes
27 answers

JavaScriptSerializer - JSON serialization of enum as string

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "name". Is there a way to get the enum as a string in my…
Omer Bokhari
  • 50,790
  • 12
  • 41
  • 54
1143
votes
43 answers

How can I represent an 'Enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. How can I represent the equivalent of an Enum in Python?
John Rutherford
  • 10,664
  • 6
  • 26
  • 31
1096
votes
13 answers

What is a typedef enum in Objective-C?

I don't think I fundamentally understand what an enum is, and when to use it. For example: typedef enum { kCircle, kRectangle, kOblateSpheroid } ShapeType; What is really being declared here?
Craig
  • 16,081
  • 11
  • 40
  • 39
1025
votes
26 answers

Convert a string to an enum in C#

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to…
Ben Mills
  • 24,294
  • 13
  • 37
  • 37
935
votes
37 answers

String representation of an Enum

I have the following enumeration: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION = 2, SINGLESIGNON = 3 } The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id…
user29964
  • 15,002
  • 19
  • 52
  • 63
893
votes
12 answers

A 'for' loop to iterate over an enum in Java

I have an enum in Java for the cardinal & intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of…
Nick Meyer
  • 36,085
  • 14
  • 61
  • 72
1
2 3
99 100