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
3 answers

Can you iterate over each possible enum value using a Qt foreach loop?

Given an enum: enum AnEnum { Foo, Bar, Bash, Baz }; Can you iterate over each of these enums using Qt's foreach loop? This code doesn't compile (not that I expected it to...) foreach(AnEnum enum, AnEnum) { // do nothing }
Cory Klein
  • 40,647
  • 27
  • 164
  • 222
12
votes
4 answers

List all bit names from a flag Enum

I'm trying to make a helper method for listing the names of all bits set in an Enum value (for logging purposes). I want have a method that would return the list of all the Enum values set in some variables. In my example [Flag] Enum HWResponse { …
PPC
  • 1,572
  • 1
  • 17
  • 38
11
votes
4 answers

Eclipse loves it, javac hates it, it's an enum, sort of, with an interface

Eclipse indigo, java 1.6 public interface I { String getName(); } /* and in another file */ public enum E implements I { E1() { String getName() { return "foo"; } }; } In Eclipse, this worked! Other classes could invoke getName() on…
bmargulies
  • 91,317
  • 38
  • 166
  • 290
11
votes
2 answers

C# - Check for attribute's existence on enum's element

I've got a situation like the following: enum Header { Sync, [OldProtocol] Keepalive, Ping, [OldProtocol] Auth, [OldProtocol] LoginData //... } I need to obtain an array of elements on which the OldProtocolAttribute is…
user1098567
  • 299
  • 3
  • 9
11
votes
2 answers

Storing and retrieving enums in SQLite with Java

I am in the need of being able to store and retrieve enums from a SQLite database, which I have solved with combined use of a wrapper class and enums, but I feel it's very poorly designed. Every time I want to add a new item to the list of enums, I…
Jan Dragsbaek
  • 7,746
  • 2
  • 23
  • 45
11
votes
3 answers

Initializing enum-indexed array?

gcc has a very nice extension in C that allows you to keep data in arrays using enum as keys: enum keys { key_alpha = 0, key_beta = 1, key_gamma = 2 }; ValType values = { [ key_alpha ] = { 0x03b1,"alpha" }, […
SF.
  • 12,380
  • 11
  • 65
  • 102
11
votes
6 answers

How to randomize enum elements?

Say you have an enum with some elements public enum LightColor { RED, YELLOW, GREEN } And would like to randomly pick any color from it. I put colors into a public List lightColorChoices = new…
James Raitsev
  • 82,013
  • 132
  • 311
  • 454
11
votes
4 answers

Should I store Enum ID/values in the database or a C# enumeration?

Say my database tables have columns like UserType, SalesType, etc. Should I have database tables with UserTypeID, userTypeName or should I just create a C# enumeration?
mrblah
  • 88,033
  • 134
  • 292
  • 404
11
votes
11 answers

Enum.Parse() or Switch

For converting a string to an enum, which of the following ways is better? This code: colorEnum color = (colorEnum)Enum.Parse(typeof(colorEnum), "Green"); or this: string colorString = ... colorEnum color; switch (colorString) { case…
Nicola Pesavento
  • 584
  • 9
  • 22
11
votes
2 answers

Doing enum cast to int

I've a problem on this code: template void dosth(T& value,const T& default_value) { if (condition) value = 10; else value = default_value; } When I call that with enum { SITUATION1, STIUATION2 }; int…
xis
  • 22,592
  • 8
  • 39
  • 55
11
votes
4 answers

Java public enum method purpose

So I have my enum public enum Sample { ValueA{ @Override public String getValue(){ return "A"; } }, ValueB{ @Override public String getValue(){ return "B"; } public void doSomething(){ } }; abstract public String…
Sign
  • 1,809
  • 20
  • 30
11
votes
4 answers

typedef enum, assiging a value within

compiling with gcc C99 I have been using enums for a while now. However, I am using some sample code to develop my application. And I came across some code like this. I have been informed this is the best practice use when using enums. But I don't…
ant2009
  • 30,351
  • 141
  • 365
  • 559
11
votes
2 answers

How to handle duplication between java enum and database table?

It is a quite common situation in our applications that some entity have to be represented by an enum: for example types, categories, status, and things like that. Often, there are conditions or flows in the code that use the values to decide…
Sam
  • 5,831
  • 6
  • 29
  • 41
11
votes
3 answers

Store enum as integer in RavenDB

I would like to store Enums as integer-values inside a RavenDB-document instead of there full-name. Doing so, I would like to ensure, that changing the name of an enum-value, does not break persistence. With FluentNHibernate, I can create a custom…
Daniel Lang
  • 6,669
  • 4
  • 25
  • 52
11
votes
2 answers

Struts 2 iterate enum

Is it possible in Struts 2 to iterate an enum using the tag ? Right now I'm doing it using a list of String, but is it possible to use an enum directly? Thanks in advance.
Paul Ulrich
  • 369
  • 1
  • 5
  • 13
1 2 3
99
100