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.

Filter by
Sorted by
Tagged with
1
vote
0answers
64 views

Enum versus ValidateSet

Currently, in my PowerShell scripts I am using [ValidateSet()] in my functions in order to limit the options for user input. Now I discovered the Enum as an alternative. I have read a couple of blogs ...
1
vote
2answers
63 views

Best way to enforce variable has certain values using enum in Python

I have a string variable named status in my code that can take on three values "Starting", "In-progress", and "Complete". A friend of mine said it was good practice to enforce that status has one of ...
0
votes
1answer
122 views

Name of this enum-based design pattern to get the type

I have been using a pattern in a lot of places (mainly C#) that I would like to know the name of. Here is an example of it in C#: public enum ThingType { A, B, C } public interface ...
2
votes
1answer
127 views

Using Enum constructors, Overridable Getters, or both in combination?

Currently an enum in our project takes properties in the constructor. They are practically guaranteed to be entirely different across every different enum. Now I'd like to add an property which is a ...
9
votes
2answers
1k views

What is the name for the integer part of a enum?

I've been writing some code comments, and I've been trying to refer to the index integer of a enum type, but I'm not sure what it's called. I'm tempted to call it the identifier, however there is ...
2
votes
3answers
154 views

What options are available to replace an `enum` with a multi-type solution?

A Quick Note I know the question title is a little odd but I haven't had much caffeine this morning so I'm having trouble thinking clearly right now. If you have any suggestions for a better title, ...
2
votes
3answers
163 views

How to model a Struct with a limited set of values that behaves like an enum?

I have a general organization problem with my code. I'm modeling DNA, and I've created a Nucleobase struct to store the "letter" of the DNA. For ease of use, I would prefer the following functionality ...
1
vote
2answers
50 views

Architecture of “tags” for contacts in a marketing system and hard-coded IDs so functions can refer to them

I have these tables: contacts, tags, contact_tags. (contact_tags has columns contact_id and tag_id.) Administrators can manually create new tags via an internal website. Administrators can also ...
2
votes
2answers
295 views

How to gradually refactor a Map with string keys to use enum keys?

I'm considering refactoring some Java code that passes around objects that implement the interface Map<String, Object>. The strings are all (as far as I know) from some fixed list of string ...
1
vote
3answers
243 views

Enum design: when is an “open” value set acceptable?

I was looking into the Enum design recommended by Microsoft (see here), and I found a statement that made me stop to think for a while: X DO NOT use an enum for open sets (such as the operating ...
0
votes
5answers
214 views

What if there is specific logic for one particular value of an enumeration class?

Background: I have seem some argument for using enumeration classes instead of enum in C#, in particular, this section from a book available at MSDN. On the references there is this "Enums are ...
1
vote
1answer
223 views

Best pattern to solve problem where objects only differ in one attribute

As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
3
votes
1answer
419 views

Is Enum Polymorphism bad for DAO Methods?

I have been going back and forth in a discussion about polymorphic enums to call different DAO methods depending on enum entry, and I haven't been able to get a common agreement on this subject. Lets ...
1
vote
4answers
223 views

How would I add up enumerator values such that any combination provides a unique number?

Backstory (You can skip) I am writing a pronunciation library for irregular words. Take something like the following: T1E1s // tee one E one es | tee one E ones 1994-1995// 1994 (minus|dash|to|) ...
1
vote
0answers
69 views

Convert between enum and discriminated union

Is there a software pattern (or some recommended guidelines) for how to convert between enum values and discriminated unions? Or more specifically: project a discriminated union onto its enum case? ...
1
vote
3answers
179 views

Using extraneous enum members for bounds/validity checking

A coworker of mine insists that this is the right way to write enums in C# public enum ExampleEnum { InvalidItem, Item1, Item2, Item3, MaxItem } We have enums defined like this ...
1
vote
1answer
158 views

How to understand and design functions with enumeration arguments that work in tandem with bitwise operators in C++ and Qt?

In the C++, there are 6 bitwise operators: Symbol Operator & bitwise AND | bitwise inclusive OR ^ bitwise XOR (eXclusive OR) << left shift >> right shift ~...
1
vote
1answer
136 views

Multilingual reference/lookup tables and reference tables with enums

I will use the reference tables for the first time so I have some questions about it. Example Table ------------------------ id | description | ------------------------ 1 | Some description1 |...
2
votes
2answers
109 views

Using a Collection with Attributes to Check and Make Decisions

I was reading on this SESE page about using a variable to indicate the object type, more specifically, an enum. The accepted answer states: When your weapon types enum just mirrors the class ...
4
votes
4answers
843 views

DB Enumerations vs App Enumerations vs Lookup tables

We started a project recently, and during the design process, we have some arguments among team members. We have to deal with enumerations so we ended up with three choices. Have the enumerations as ...
-1
votes
1answer
218 views

Few unusual C/C++ declarations

I came across this Enum and Struct declarations in a project supposedly done by an expert. The declarations / definitions are little different than what im used to so far. enum EnumKeys { KEY_MENU ,...
1
vote
1answer
174 views

Keep enumeration in DB when data associated with it is not storable in DB

I've seen several questions on this website explaining that enums should be kept in the database when there is one (this one for example). However, I feel like my case is different and there are extra ...
0
votes
1answer
483 views

Enum as part of domain - enum + extension or consolidate into one class?

I have a program where the domain is focused around programs. As part of the domain, I have a 'ProgramType', which is an enum formed mostly via a string from the database but also via a bit of logic. ...
4
votes
1answer
4k views

How to improve a pattern 'enum class with methods'

Over many years, I always find myself reconsidering this design, so I wanted to get some feedback on my solution to it. Problem: I need a limited amount of objects = instances from a class, and I ...
1
vote
1answer
114 views

What is the solution of enum based dynamic states and actions used in a project

I have recently started working on a project where we have a workflow engine which has flexibility to add dynamic states and corresponding actions for each state and all these are stored in database. ...
2
votes
1answer
2k views

Enum or reference table when dealing with maps

I am storing information about Widgets in my database, and each Widget has one (non-unique) transformation function associated with it. My problem is how to associate Widgets with their transformation ...
1
vote
1answer
316 views

Enum or dictionary for inmemory parameter

I have a table UserItems {ID, UserID, ItemID} Items must be in RAM (not in db table). What is better for this? As enum with attributes or as dictionary of items?: enum Items { [InternalParam("...
4
votes
3answers
167 views

Changing bot application state (Starting → Started → Stopping → Stopped)

I am developing a bot which I need to start and stop at will. I have 2 buttons in my form, StartButton and StopButton which change the state. The Bot has 4 possible states: starting, started, ...
17
votes
9answers
38k views

Should one test the values of an enum using unit tests?

If you have an enum with values only (no methods as one could do in Java), and this enum is part of the business definition of the system, should one write unit tests for it? I was thinking that they ...
11
votes
7answers
1k views

Is it a good practice to have a special value “ALL” in an enum

I'm developing a new service in a micro-services environment. This is a REST service. For simplicity, let's say that the path is: /historyBooks And the POST method for this path creates a new history ...
0
votes
1answer
178 views

What's the most idiomatic way to make a collection of enum.Enum in Python?

I have an Enum in Python that looks something like this: import enum class Color(enum.Enum): red = 'red' blue = 'blue' yellow = 'yellow' puce = 'puce' chartreuse = 'chartreuse' ...
0
votes
3answers
3k views

Utility class or helper function in class?

I'm Working with php and we using enum's by create an Abstract class that mocking enum behavior - but my question is cross-languages. Where all the helper functions like toString, getEnumTypes, ...
4
votes
2answers
452 views

Is there an Ideal way of storing namespace-related elements?

In the title, with namespace-related elements, I refer to Enums, Delegates and other elements that do not belong to a single class, but to the whole namespace, or application. I know that I can cram ...
2
votes
3answers
343 views

If an enum is used without bitwise operation, and all of its values are predefined individually, is it violating open closed principle?

For example, if I use an enum, which all values are manually defined individually, for example: public enum MyNum{ Zero(0), One(1), Two(2); private final int value; MyNum(int ...
13
votes
4answers
5k views

Is it wrong to use flags for “grouping” enums?

My understanding is that [Flag] enums are typically used for things that can be combined, where the individual values aren't mutually exclusive. For example: [Flags] public enum SomeAttributes { ...
1
vote
2answers
5k views

C# is it better to use string or an enum as a key?

Example 1 (used at the moment) If (BodyPart["Right Leg"].BodyStatusEffects["Active"].Active) { BodyPart["Right Leg"].BodyStatusImpacts["Poisoned"].Active = false; BodyPart["Torso"]....
4
votes
2answers
880 views

Semantic Versioning in OpenAPI with string “enum”: new values OK?

My team is preparing to add new capabilities to an OpenAPI contract and our implementation of it. There are pre-existing clients. We are planning to take our API from v1.1 to v1.2 while fully ...
0
votes
1answer
3k views

Is it a good practice to write an enum as a class file in java?

I am constructing an enum Flower which contains the fixed set of constants for Flowers as described below. I'm writing it in a separate file and importing it to other classes so that it could be used ...
0
votes
3answers
2k views

Why do the following enum fields extend their base class or base enum?

Regarding enums in java how I understood is Sample enum public enum Strategy { STRATEGY_A { @Override void execute(){ System.out.print("Executing strategy A"); ...
2
votes
2answers
637 views

Static per-enum data: constructor, set in initialiser or override getter?

I have an enum with > 10 items each having 8 static properties. Contrived example: enum JavaTypes { INTEGER, BOOLEAN, STRING, ...; boolean isPrimitive() { } boolean ...
2
votes
1answer
4k views

ASP.NET Web API: enum or strings

I have an ASP.NET Web API application. It uses enums for some fixed sets of states or types. Now I need to extend enum to support more values. But it will break backwards compatibility so I need to ...
2
votes
2answers
226 views

What should you replace an enumeration with if values are to be provided by plugins?

I'm working on a piece of software which generates configuration data for certain hardware and currently needs to be adapted each time the hardware is released in a new version by an external company (...
1
vote
2answers
6k views

Mapping enum values into regexes

I'm doing some code cleanup and I'm looking at my regexes. I have an extremely simple one: (ARA|CHI|FRE|GER|ITA|JPN|RUS|SPA)\s[0-9]{3}-[0-9]{2} It basically validates course identifiers for a ...
0
votes
0answers
184 views

Can I keep my code loosely coupled with public-facing enums?

I have some enums in a concrete API/library that will be publicly used by application projects. My problem here, is that I cannot write an interface to these enums (that I know of). I should have ...
1
vote
1answer
75 views

Is an all-encompassing enumerator appropriate?

I will illustrate the problem with a specific case. Suppose we have a bit-flag style enumeration type defining different kinds of validations. It's tempting to define an enumerator like ...
3
votes
3answers
518 views

Enums and single responsibility principle (SRP)

I have been experimenting lately with enums, and I found out that in Java they can do much more than simply representing a fixed set of constants. Now, I am thinking about creating a new enum for my ...
33
votes
5answers
7k views

Why do we need enums in dynamically typed languages?

I was reading some code here and saw that an enum is used to store names of html tags. Why do we ever need to do this? What benefit do I get using this strategy? I know that how useful enums are in ...
1
vote
2answers
4k views

What is the best data structure to store 2 keys and 3 values?

Well, I am having two keys and 3 values for that. Say key1,key2,value1,value2,value3. In future may the values can be increased like value4, value5 so on. I need to get the values(value1,value2,...
1
vote
0answers
102 views

How to structure enum data to achieve the desired effect

I'm making a framework for building simple html websites for an embedded system and I want to make it bulletproof in a way that a user can't make mistakes in building the html document. As I've ...
1
vote
2answers
255 views

Would adding enums to my class make my code more compact or efficient?

I am working on a C# class library for my colleagues to use when programming industrial cameras. I'm trying to determine both the most compact and elegant method to write the class. The commands are ...