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
12
votes
4 answers

How to tell if a PropertyInfo is of a particular enum type?

I have the following code: public class DataReader where T : class { public T getEntityFromReader(IDataReader reader, IDictionary FieldMappings) { T entity = Activator.CreateInstance(); Type entityType =…
griegs
  • 22,002
  • 28
  • 113
  • 201
12
votes
5 answers

Enum declared outside class scope

I went to this interview for a software developer position and they gave me a test with some corner-case-code situations, with usually 4 options to choose. One of the questions had an enum declared outside the class scope, I promptly checked the…
Alberto Zaccagni
  • 28,473
  • 10
  • 71
  • 102
12
votes
2 answers

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

The following is from the Implementation Note section of Java doc of EnumMap : Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. I have…
Geek
  • 23,609
  • 39
  • 133
  • 212
12
votes
1 answer

Is there a Scala equivalent of EnumSet/EnumMap?

In Java we have two nice classes: EnumSet for sets of enums and EnumMap for a map whose keys are enums. EnumSet is represented as a 64-bit word (or an array of 64-bit words) and EnumMap as an array of values, both indexed by the ordinal numbers of…
Petr
  • 60,177
  • 8
  • 136
  • 295
12
votes
2 answers

how to compare enum values

I have a question about enum C. I defined an enum in the following way: typedef enum { Hello1 = 1, Hello2 = 2, Hello3 = 3 }Hello Hello hello; int value = 3; then how to compare the value with the value in Hello? for…
user707549
12
votes
5 answers

sorting enum for UI purpose

Say we have a UI and in this UI we have a dropdown. This dropdown is filled with the translated values of an enum. Bow, we have the possibility to sort by the int-value of the enum, by the name of the enum, and by the translated name of the…
karlis
  • 852
  • 2
  • 12
  • 22
12
votes
7 answers

Is there any well-known paradigm for iterating enum values?

I have some C++ code, in which the following enum is declared: enum Some { Some_Alpha = 0, Some_Beta, Some_Gamma, Some_Total }; int array[Some_Total]; The values of Alpha, Beta and Gamma are sequential, and I gladly use the following…
SadSido
  • 2,443
  • 20
  • 34
12
votes
1 answer

Define an "Unknown" or "NULL" value in an enum

I am defining a custom typedef Elements as follows.... typedef enum { Ar, Cl, F, He, H, Kr, Ne, N, O, Rn, Xe } Element; I want to check a variable of type Element has not been set (essentially just check…
Nick Jones
  • 513
  • 1
  • 3
  • 17
12
votes
3 answers

Execution order of of static blocks in an Enum type w.r.t to constructor

This is from Effective Java : // Implementing a fromString method on an enum type private static final Map stringToEnum = new HashMap(); static { // Initialize map from constant name to enum constant …
Geek
  • 23,609
  • 39
  • 133
  • 212
12
votes
1 answer

Should mapping value be declared in a constant or as an enum?

I see this scattered throughout code base: @RequestMapping(value = "myValue") I would prefer to use something like this: @RequestMapping(value = Constants.myValue) It seems to break DRY using the actual String value within @RequestMapping…
blue-sky
  • 45,835
  • 124
  • 360
  • 647
12
votes
9 answers

What is Enum useful for?

After reading some questions and answers about enum I don't find it really useful... It's something between class and a variable but I know where can I use it so it would be more useful then a class, or a couple of variables for example.
Roni Copul
  • 179
  • 1
  • 1
  • 8
12
votes
2 answers

Store an ordering of Enums in Java

In java, an EnumSet stores the items it contains in a bitmask / bit vector using a long (RegularEnumSet) or long[] (JumboEnumSet). I have now come across a use case where I have many thousand domain Objects (let's call them Node), each of which will…
Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
12
votes
2 answers

using value of enum in g:select when enum is attribute of selection object

Example: batchTag is an enumerated type attribute of a batchRange, with values like so: JAN1 "January Biweekly 1", JAN2 "January Biweekly 2", etc. I want to display the VALUE of the batchTag in the select, IOW, the select should contain "January…
Alexx
  • 3,352
  • 6
  • 28
  • 39
12
votes
7 answers

Check if a value is defined in an C enum?

Assuming that I have this: enum { A = 0x2E, B = 0x23, C = 0x40 } it's possible check if x is defined into enum? I'm doing it manually: int isdef = (x == A || x == B || x == C); But I want to something more dynamic. GCC-extensions are welcome too.
Jack
  • 14,916
  • 47
  • 136
  • 245
12
votes
3 answers

What is the syntax to assign multiple enum values to a property in F#?

I am writing a ServiceStack webservice in F# and need to limit some of the features (removing SOAP support for instance). In C# I am using the pipe operation to assign multiple Enums (ServiceStack.ServiceHost.Feature) to the EnableFeatures property…
John B Fair
  • 185
  • 8
1 2 3
99
100