enums

typechecking provided on enum

I would expect the following code snippet to complain about trying to assign something other that 0,1,2 to a Color variable. But the following does compile and I get the output Printing:3 3 Can anybody explain why? Is enum not meant to be a true user-defined type? Thanks. enum Color { blue=0,green=1,yellow=2}; void print_color(Colo...

How do I overload an operator for an enumeration in C#?

I have an enumerated type that I would like to define the >, <, >=, and <= operators for. I know that these operators are implictly created on the basis of the enumerated type (as per the documentation) but I would like to explictly define these operators (for clarity, for control, to know how to do it, etc...) I was hoping I could do ...

The right file name for an enum?

I have a DataType (text, numeric, date, boolean, money) enum and I need to put that in a file. How should I call the file and where in my DataAccessLayer.Generic namespace should I put the file into? Right now the file is called enum.vb and is in the DataAccessLayer.Generic.DataTypes namespace. The Result is DataAccessLayer.Generic.DataT...

enum depending on Type T

I have a generic class that needs to limit an enum depending on the type defined: public enum ConditionOperatorsString { None, Like, Equal } public enum ConditionOperatorsDate { None, Equal, BeforeThan, AfterThan } public class Condition<T> { public T Value { get; set; } public ConditionOperatorsString Operator { get; set; } publi...

How to find out all possible values of an enum?

Say I have an enum type MyEnum. Is there a way in C# to get a list of all possible values for an enum of type MyEnum? ...

C# Keys Enumeration Confused: Keys.Alt or Keys.RButton | Keys.ShiftKey | Keys.Alt

I was trying to test whether the Alt key was pressed. I had a check similar to: private void ProcessCmdKey(Keys keyData) { if (keyData == Keys.Alt) { System.Console.WriteLine ("Alt Key Pressed"); } } Anyways needless to say when I breakpointed when I had pressed the Alt key the debugger told me the key that was pressed was actu...

Scala Enumeration and readResolve

Scala's Enumeration class and the Enumeration.Val readResolve method do not appear to work as they should do (possibly related to this entry in Scala trac). Here is a program provided by *Steve Bendiola" to illustrate the problem: @serializable object InvestmentType extends Enumeration { val Debt = Value("DEBT") val Future = Value("...

What's the equivalent of Java's enum in C#?

What's the equivalent of Java's enum in C#? ...

Is there a reason to use enum to define a single constant in C++ code?

The typical way to define an integer constant to use inside a function is: const int NumbeOfElements = 10; the same for using within a class: class Class { ... static const int NumberOfElements = 10; }; It can then be used as a fixed-size array bound which means it is known at compile time. Long ago compilers didn't support th...

C# TypeConverter long to enum type fails on ChangeType

I'm fairly new to C# and .NET - I'm trying to get conversion to work from an integer to an enum. The conversion must be performable by ChangeType (outside of my demo below this is fixed as it's within the data binding framework) and from what I've read it should work with what I'm doing, but I get an exception and if I place breakpoints...

What is the best way to store 10 options in a mysql database?

I am modifying my PHP network's code to have "user roles" like wordpress here is my plan so far 0 = registred non email verified user 1 = registed and verified email 2 = moderator 3-9 = nothing yet 10= admin In my PHP code I will use an array like this that will set what a role number does. $user_role['10'] I was thinking of stori...

In Java, when is the constructor for an enumerated constant invoked?

To use a contrived example in Java, here's the code: enum Commands{ Save("S"); File("F"); private String shortCut; private Commands(String shortCut){ this.shortCut = shortCut; } public String getShortCut(){ return shortCut; } } I have the following test/driver code: public static void main(String args[]){ System.ou...

Combining extension methods

I'm trying to write 2 extension methods to handle Enum types. One to use the description attribute to give some better explanation to the enum options and a second method to list the enum options and their description to use in a selectlist or some kind of collection. You can read my code up to now here: <Extension()> _ Public ...

How do I create enumerated types in MATLAB?

Are there enumerated types in MATLAB? If not, what are the alternatives? ...

Enumerate over an enum in C++

In C++, Is it possible to enumerate over an enum (either runtime or compile time (preferred)) and call functions/generate code for each iteration? Sample use case: enum abc { start a, b, c, end } for each (__enum__member__ in abc) { function_call(__enum__member__); } Plausible duplicates: C...

Self-referential enum attributes in a C# dynamic assembly

Consider the following code: [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum, AllowMultiple = true)] public class TransitionToAttribute : Attribute { public readonly object Next; public TransitionToAttribute(object next) { Next = next; } } [TransitionToAttribute(Directe...

Parsing enums from property file

I have a simple key-value property file where I need to parse a value which is then to be assigned to an enum type. What is the best way to do this? The only thing that comes up to my mind is something like iterating through all the possible values of the enums.toString and see if it equals any of them. ...

Is there a naming convention for enum values that have a leading digit?

I have a c# enumeration that looks like this: public enum EthernetLinkSpeed { [Description("10BASE-T")] _10BaseT, [Description("100BASE-T")] _100BaseT, [Description("1000BASE-T")] _1000BaseT, [Description("Disconnected")] Disconnected } I added a leading underscore to each value to make the compiler...

Enum Extension Method – ToDataSource

I have tried to add an extension method to an enum which converts it to a DataTable so that it can be bound to a DropDownList which works fine. public enum Response { Yes = 1, No = 2, Maybe = 3 } public static DataTable ToDataSource(this Enum e) { Type type = e.GetType(); DataTable dt = new DataTable(); dt.C...

linking methods from an enum to member variables of client class

Hello, The following enum structure performs certain operations while remaining agnostic to the client class (for encapsulation reasons) public enum MyEnum implements Commands{ A{ public int method1(int varY) { return varY+2; } public MyEnum method2(){ return MyEnum.B; ...