3,075 questions
1812
votes
15
answers
726k
views
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 ...
1140
votes
43
answers
1.3m
views
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?
2407
votes
32
answers
1.7m
views
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 ...
600
votes
29
answers
451k
views
Getting attributes of Enum's value
I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum:
using System.ComponentModel; // for ...
1472
votes
31
answers
813k
views
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 "...
481
votes
11
answers
176k
views
How to bind RadioButtons to an enum?
I've got an enum like this:
public enum MyLovelyEnum
{
FirstSelection,
TheOtherSelection,
YetAnotherOne
};
I got a property in my DataContext:
public MyLovelyEnum VeryLovelyEnum { get; ...
768
votes
9
answers
552k
views
Why is enum class considered safer to use than plain enum?
I heard a few people recommending to use enum classes in C++ because of their type safety.
But what does that really mean?
612
votes
29
answers
425k
views
What are enums and why are they useful?
While browsing Stack Overflow I came across java singleton instantiation. It references an enum being used in a singleton pattern. I have been learning to write programs for couple of years now, and ...
179
votes
7
answers
306k
views
What is the size of an enum in C?
I'm creating a set of enum values, but I need each enum value to be 64 bits wide. If I recall correctly, an enum is generally the same size as an int; but I thought I read somewhere that (at least in ...
99
votes
5
answers
101k
views
Generic type to get enum keys as union string in typescript?
Consider the following typescript enum:
enum MyEnum { A, B, C };
If I want another type that is the unioned strings of the keys of that enum, I can do the following:
type MyEnumKeysAsStrings = keyof ...
504
votes
5
answers
775k
views
How to get C# Enum description from value? [duplicate]
I have an enum with Description attributes like this:
public enum MyEnum
{
Name1 = 1,
[Description("Here is another")]
HereIsAnother = 2,
[Description("Last one")]
LastOne = 3
}
I ...
447
votes
29
answers
562k
views
How can I iterate over an enum?
I just noticed that you can not use standard math operators on an enum such as ++ or +=.
So what is the best way to iterate through all of the values in a C++ enum?
234
votes
6
answers
165k
views
Implementing Singleton with an Enum (in Java)
I have read that it is possible to implement Singleton in Java using an Enum such as:
public enum MySingleton {
INSTANCE;
}
But, how does the above work? Specifically, an Object has to be ...
101
votes
20
answers
127k
views
Easy way to use variables of enum types as string in C?
Here's what I am trying to do:
typedef enum { ONE, TWO, THREE } Numbers;
I am trying to write a function that would do a switch case similar to the following:
char num_str[10];
int ...
735
votes
45
answers
1.1m
views
How to get names of enum entries?
I would like to iterate a TypeScript enum object and get each enumerated symbol name, for example:
enum myEnum { entry1, entry2 }
for (var entry in myEnum) {
// use entry's name here, e.g., "...
182
votes
7
answers
27k
views
Java Enum definition
I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum:
class Enum<E extends Enum<E>>
Could someone explain how to interpret this type ...
1786
votes
8
answers
1.0m
views
How to loop through all enum values in C#? [duplicate]
public enum Foos
{
A,
B,
C
}
Is there a way to loop through the possible values of Foos?
Basically?
foreach(Foo in Foos)
2226
votes
15
answers
1.1m
views
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 ...
134
votes
11
answers
195k
views
How to convert enum names to string in c
Is there a possibility to convert enumerator names to string in C?
222
votes
11
answers
196k
views
Most common C# bitwise operations on enums
For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to ...
210
votes
13
answers
82k
views
C# vs Java Enum (for those new to C#)
I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#. I'm trying to come up to speed in C#, and noticed enums used in several places in my new ...
112
votes
4
answers
104k
views
How to use enum values in f:selectItem(s)
I want to make a selectOneMenu dropdown so I can select a status on my question. Is it possible to make the f:selectItem more flexible considering what happens if the order of the enums changes, and ...
615
votes
42
answers
334k
views
How to enumerate an enum with String type?
enum Suit: String {
case spades = "♠"
case hearts = "♥"
case diamonds = "♦"
case clubs = "♣"
}
For example, how can I do something like:
for suit in Suit {
// do something with ...
136
votes
15
answers
315k
views
C++: Print out enum value as text
If I have an enum like this:
enum Errors {
ErrorA = 0,
ErrorB,
ErrorC,
};
Then I want to print it out to console:
Errors anError = ErrorA;
std::cout << anError; // 0 will be printed
...
194
votes
18
answers
174k
views
What's the advantage of a Java enum versus a class with public static final fields?
I am very familiar with C# but starting to work more in Java. I expected to learn that enums in Java were basically equivalent to those in C# but apparently this is not the case. Initially I was ...