Questions tagged [enum]
Enum (also enumeration) 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, and are often predefined with an implied ordering.
134 questions
10
votes
3
answers
22k
views
Best approach for multilingual Java Enum
I'm running into issues with an approach I am taking and am now wondering if I just started down the wrong path and should rethink my approach. Here is what I attempting.
I would like to use an Enum ...
2
votes
1
answer
5k
views
Several enum classes that declare constants with the same names
I'm working on a Java library for sorts for Magic: the Gathering. Well, without going into a lot of detail, in the game there are five different colors of magic: white, blue, black, red, and green. ...
10
votes
9
answers
12k
views
Why isn't there a next operation on enums?
In most popular programming languages like Java and C# there is a way to define enums, which are essentially datatypes with a fixed set of values, e.g. DayOfWeek.
The problem is, given a value, e.g. ...
2
votes
3
answers
8k
views
In what situation do Entity Framework enums become useful?
I am working on a project where there will be plenty of static options being stored in the database. I looked at using Enums for this, but do not see how they could be useful.
They do not create any ...
2
votes
2
answers
1k
views
Allowing enum to express a valid combination of values
Suppose in a manufacturing environment, there are certain stock materials available for use in a product. For example, there are only a few different sizes of copper tube, each having a specific ...
6
votes
5
answers
17k
views
How to unit test a missing case in a switch statement where all cases are true
I often use enum types in my code with a switch to apply logic to each type. In these cases it's important that each enum has code implemented.
For example;
public enum eERROR
{
REQUIRED,
...
4
votes
1
answer
3k
views
constants/enums in API
What are a few ways constants and enums are dealt with when creating an API Client? I'm writing a client in python for our API and I've hit a sticking point with this. We use a lot of mappings to ...
3
votes
2
answers
1k
views
Are the IETF BCP 47 language tags defined as enums anywhere in JDK?
Are the IETF BCP 47 language tags defined as enums anywhere in JDK?
For Locale.forLanguageTag() we pass values like fr-FR, jp-JP etc.
Are there any enums already provided by JDK for it? Or should ...
4
votes
1
answer
5k
views
Which is better to use? (Enum or Constant)
I have my enum like this.
enum Size{PAGE_SIZE=20,
HEADER_SIZE=30
};
I only use them as constants(No enum variable created) in my program.
i.e= int x = PAGE_SIZE + 20;
So what is the better ...
37
votes
3
answers
42k
views
How to represent (enum) types in a public API
I am working on a simple API that I want to use for my own client, and to open to the public in the future.
I have "Item" objects which can have different "types". The type is a C &...
9
votes
3
answers
10k
views
Compatibility of Enum Vs. string constants
I was recently told that using Enum:
public enum TaskEndState { Error, Completed, Running }
may have compatibility/serialization issues, and thus sometimes it's better to use const string:
public ...
3
votes
1
answer
2k
views
Could someone explain HaXe enums?
I have AS3/PHP background and some basic understaning of enums from C++. Saying shortly: I read this:
http://haxe.org/ref/enums
http://haxe.org/doc/cross/more_on_enum
And though - good heavens! ...
7
votes
7
answers
9k
views
In what situations does it make sense to use an enumeration when writing object-oriented code?
Enumerations1 are often associated with procedural code rather than object-oriented code. They tend to give rise to similar switch statements scattered through the code, and in general, these are ...
1
vote
3
answers
1k
views
Does an in-code enumeration provide a stronger domain model than a static table?
Does an in-code enumeration provide a stronger domain model than a static database table?
As an example, say I have an Marble entity, with a Color attribute. The color attribute has a finite set of ...
0
votes
2
answers
1k
views
use of Enum with flags in practice?
I just have read some stuff on enum today. Use of flags with enum was something interesting and new for me. But often practice and theoretical uses are different. I go through many articles they ...
9
votes
2
answers
573
views
Why is Flags Enumeration considered an intermediate skill?
I was reading this article: Designing Flags Enumerations @ msdn and it says
Combining flags enumeration values is an intermediate skill that should not be required for developers implementing common ...
77
votes
8
answers
110k
views
Using scoped enums for bit flags in C++
An enum X : int (C#) or enum class X : int (C++11) is a type that has a hidden inner field of int that can hold any value. In addition, a number of predefined constants of X are defined on the enum. ...
9
votes
4
answers
19k
views
Nested Enum type in C++ or C#?
I've come across a recurring issue in a few of my recent projects in which I find myself using enums to represent state, or type, or something else, and I need to check against a few conditions. Some ...
4
votes
2
answers
6k
views
Is it possible to avoid enormously big switch in that case? [duplicate]
I'm writing a simple chess-related code with intention to write it clearly (performance doesn't matter at all). And this method I have doesn't look clean to me at all:
public static Piece ...
5
votes
2
answers
615
views
Do I really want a dynamic Enum, or something else? Better caching? A Struct?
This question is asked on SO and this site often it looks like. "How do I do a dynamic enum?" Answers range from you shouldn't to possible compile time solutions. Now my question is do I want a ...
14
votes
3
answers
10k
views
What are the downsides of implementing a singleton with Java's enum?
Traditionally, a singleton is usually implemented as
public class Foo1
{
private static final Foo1 INSTANCE = new Foo1();
public static Foo1 getInstance(){ return INSTANCE; }
private ...
16
votes
8
answers
32k
views
Is it a bad practice to include all the enums in one file and use it in multiple classes?
I'm an aspiring game developer, I work on occasional indie games, and for a while I've been doing something which seemed like a bad practice at first, but I really want to get an answer from some ...
12
votes
4
answers
7k
views
Keeping an enum and a table in sync
I'm making a program that will post data to a database, and I've run into a pattern that I'm sure is familiar: A short table of most-likely (very strongly likely) fixed values that serve as an enum. ...
6
votes
3
answers
7k
views
How to deal with almost the same enums?
I need to define enums in several classes. The majority of fields are the same in all of the enums. But one has one or two more fields, another has fewer fields. Now I wonder what is the best way to ...
5
votes
4
answers
6k
views
Should I create an Enum mapping to my database table
I have a database table containing a list of systems relevant to the tool I am building, mostly in-house applications, or third-party systems we receive data from. This table is added to infrequently, ...
2
votes
2
answers
3k
views
Is switch-case over enumeration bad practice?
I have an enumeration with the commands Play, Stop and Pause for a media player. In two classes I do a switch-case over the received commands. The player runs in a different thread and I deliver the ...
13
votes
2
answers
40k
views
Best practice for packing Java enums? [closed]
What is the best practice for packaging Java enums?
is it separate file for each enum?
or
having same file for all the enums?
What are the pros and cons ?
0
votes
1
answer
262
views
How far should I expose this status enum?
I wrote a little app to manage an arbitrary series of tasks (e.g., call a SQL sproc and capture out-vars, run another app, run an SSIS package) with dependencies between tasks. Each task has a status ...
7
votes
2
answers
9k
views
Why would one ever want to use a synchronized method in Enum?
I stumbled upon this question and a couple of other along the same lines.
While we know creation of enum is thread safe and enums are by birth singletons .
It kind of confuses me that why would ...
23
votes
5
answers
33k
views
switch statement - handling default case when it can't be reached
If I'm using a switch statement to handle values from an enum (which is owned by my class) and I have a case for each possible value - is it worth adding code to handle the "default" case?
enum ...
6
votes
3
answers
34k
views
How to use a switch statement with enum efficiently?
I would like to know how I can use a switch statement with enum values for the following scenarios:
I am making a small program for a flight reservation system. The program is meant to enter certain ...
1
vote
1
answer
2k
views
Enum with FlagsAttribute or IEnumerable<Enum>/ISet<T>
Which is the currently-accepted best practice in terms of C# enum usage, a bitwise [Flags] Enum (compared using | and &) or an IEnumerable of enum values? I'm thinking both from a code-conciseness ...
6
votes
2
answers
2k
views
Java: would you use an EnumSet in this case?
I have this weird scenario in which some Java property is used to build and HQL query, and the type of the field is Boolean, i.e. it is boxed. I wondered why because I don't really like to think of ...
8
votes
3
answers
4k
views
What are the common misuses of "enum" in C?
I have seen C code where people used enum heavily. But all it does is confuse others. In many places plain integers can do the same thing with less ambiguity. What are the common misuses of enum?