How to model type-safe enum types?

I must say that the example copied out of the Scala documentation by skaffman above is of limited utility in practice (you might as well use case objects). In order to get something most closely resembling a Java Enum (i.e. with sensible toString and valueOf methods — perhaps you are persisting the enum values to … Read more

Coding Conventions – Naming Enums

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So enum Fruit {APPLE, ORANGE, BANANA, PEAR}; There is no reason for writing FruitEnum any more than FruitClass. You are just wasting four (or five) characters that add no information. This approach … Read more

Enum Naming Convention – Plural

Microsoft recommends using singular for Enums unless the Enum represents bit fields (use the FlagsAttribute as well). See Enumeration Type Naming Conventions (a subset of Microsoft’s Naming Guidelines). To respond to your clarification, I see nothing wrong with either of the following: public enum OrderStatus { Pending, Fulfilled, Error }; public class SomeClass { public … Read more

Enum ToString with user friendly strings

I use the Description attribute from the System.ComponentModel namespace. Simply decorate the enum: private enum PublishStatusValue { [Description(“Not Completed”)] NotCompleted, Completed, Error }; Then use this code to retrieve it: public static string GetDescription<T>(this T enumerationValue) where T : struct { Type type = enumerationValue.GetType(); if (!type.IsEnum) { throw new ArgumentException(“EnumerationValue must be of Enum … Read more

How to implement Enums in Ruby?

Two ways. Symbols (:foo notation) or constants (FOO notation). Symbols are appropriate when you want to enhance readability without littering code with literal strings. postal_code[:minnesota] = “MN” postal_code[:new_york] = “NY” Constants are appropriate when you have an underlying value that is important. Just declare a module to hold your constants and then declare the constants … Read more

Convert from enum ordinal to enum type

To convert an ordinal into its enum represantation you might want to do this: ReportTypeEnum value = ReportTypeEnum.values()[ordinal]; Please notice the array bounds. Note that every call to values() returns a newly cloned array which might impact performance in a negative way. You may want to cache the array if it’s going to be called … Read more

How can I loop through enum values for display in radio buttons? [duplicate]

Two options: for (let item in MotifIntervention) { if (isNaN(Number(item))) { console.log(item); } } Or Object.keys(MotifIntervention).filter(key => !isNaN(Number(MotifIntervention[key]))); (code in playground) Edit String enums look different than regular ones, for example: enum MyEnum { A = “a”, B = “b”, C = “c” } Compiles into: var MyEnum; (function (MyEnum) { MyEnum[“A”] = “a”; MyEnum[“B”] … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)