enums

Most common C# bitwise operations

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 have. For example: flags = flags | FlagsEnum.Bit4; // Set bit 4. or if ((flags == FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a les...

How to get the underlying value of an enum

I have the following enum declared: public enum TransactionTypeCode { Shipment = 'S', Receipt = 'R' } How do I get the value 'S' from a TransactionTypeCode.Shipment or 'R' from TransactionTypeCode.Receipt ? Simply doing TransactionTypeCode.ToString() gives a string of the Enum name "Shipment" or "Receipt" so it doesn't cut the musta...

C#: How to enumerate an enum?

How can you enumerate a enum in C#? e.g. the following does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { DoSomething(suit); } } It gives the compile time error: 'Suit' is a 'type' but is used like a 'variable' It fails on the Sui...

I need a helper method to compare a char Enum and a char boxed to an object

I have an enum that looks as follows: public enum TransactionStatus { Open = 'O', Closed = 'C'}; and I'm pulling data from the database with a single character indicating - you guessed it - whether 'O' the transaction is open or 'C' the transaction is closed. now because the data comes out of the database as an object I am having a h...

How should I best emulate and/or avoid enum's in Python?

I've been using a small class to emulate Enums in some Python projects. Is there a better way or does this make the most sense for some situations? Class code here: class Enum(object): '''Simple Enum Class Example Usage: >>> codes = Enum('FOO BAR BAZ') # codes.BAZ will be 2 and so on ...''' def __init__(self, names): for number, na...

Should I use #define, enum or const?

In a C++ project I'm working on I have a flag kind of value which can have 4 values. Those 4 flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each Record I wish to keep this attribute, so I could use enum: enum { xNew, xDeleted, xModified, xE...

How to build interface for such enum

I have the following enum: public enum Status implements StringEnum{ ONLINE("on"),OFFLINE("off"); private String status = null; private Status(String status) { this.status = status; } public String toString() { return this.status; } public static Status find(String value) { for(Status status : Status.values()) { if...

Difference between Enum and Define Statements

What's the difference between using a define statement and an enum statement in C/C++? (and is there any difference when using them with either C or C++?) For example, when should one use enum {BUFFER = 1234}; over #define BUFFER 1234 Thanks ...

How to use Java reflection when the enum type is a Class?

I was using an enum in which the constant was a Class. I needed to invoke a method on the constant but could not introduce a compile time dependency and the enum was not always available at runtime (part of optional install). Therefore, I wanted to use reflection. This is easy, but I hadn't used reflection with enums before. The en...

iterating over Enum constants in JSP

Hi, I have an Enum like this package com.example; public enum CoverageEnum { COUNTRY, REGIONAL, COUNTY } I would like to iterate over these constants in JSP without using scriptlet code. I know I can do it with scriptlet code like this: <c:forEach var="type" items="<%= com.example.CoverageEnum.values() %>"> ${type}...

Java: Enum parameter in method

Hi guys, I have a method lets say: private static String drawCellValue(int maxCellLength, String cellValue, String align) { } and as you can notice, I have a parameter called align. Inside this method I'm going to have some if condition on whether the value is a 'left' or 'right'.. setting the parameter as String, obviously I can pass...

How much memory do Enums take?

For example if I have an Enum with two cases, does it make take more memory than a boolean? Languages: Java, C++ ...

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 process_numbers_str(Numbers num) { switch(num) { case ONE: case TWO: case THREE: { strcpy(num_str, num); //some way to get the ...

Max and min values in a C++ enum

Is there a way to find the maximum and minimum defined values of an enum in c++? ...

Are C++ enums signed or unsigned?

Are C++ enums signed or unsigned? And by extension is it safe to validate an input by checking that it is <= your max value, and leave out >= your min value (assuming you started at 0 and incremented by 1)? ...

Determining if enum value is in list (C#)

Hello, I am building a fun little app to determine if I should bike to work. I would like to test to see if it is either Raining or Thunderstorm(ing). public enum WeatherType : byte { Sunny = 0, Cloudy = 1, Thunderstorm = 2, Raining = 4, Snowing = 8, MostlyCloudy = 16 } I was thinking I could do something like: WeatherType _badWeat...

How to create enum object from its type and name of the value?

I have a type (System.Type) of an enum and a string containing enumeration value to set. E.g. given: enum MyEnum { A, B, C }; I have typeof(MyEnum) and "B". How do I create MyEnum object set to MyEnum.B? ...

How to convert VB.net interface with enum to C#

I have the following VB.net interface that I need to port to C#. C# does not allow enumerations in interfaces. How can I port this without changing code that uses this interface? Public Interface MyInterface Enum MyEnum Yes = 0 No = 1 Maybe = 2 End Enum ReadOnly Property Number() As MyEnum End In...

Proper naming of enum values for C#

How do you think, is it a good idea to have such an enum: enum AvailableSpace { Percent10, Percent20, SqF500, SqF600 } The question is about the semantics of the values names, i.e. both percentage and square feet. I really believe that it's not a good idea, but I could not find and guidelines, etc. in support of this. EDI...

Sharing Enum with WCF Service

I have few different applications among which I'd like to share a C# enum. I can't quite figure out how to share an enum declaration between a regular application and a WCF service. Here's the situation. I have 2 lightweight C# destop apps and a WCF webservice that all need to share enum values. Client 1 has Method1( MyEnum e, s...