Use reserved keyword a enum case

From the Swift Language Guide (Naming Constants & Variables section) If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice. enum MyEnum { … Read more

Java Enum getDeclaringClass vs getClass

Java enum values are permitted to have value-specific class bodies, e.g. (and I hope this syntax is correct…) public enum MyEnum { A { void doSomething() { … } }, B { void doSomethingElse() { … } }; } This will generate inner classes representing the class bodies for A and B. These inner classes … Read more

Maximum number of enum elements in Java

From the class file format spec: The per-class or per-interface constant pool is limited to 65535 entries by the 16-bit constant_pool_count field of the ClassFile structure (ยง4.1). This acts as an internal limit on the total complexity of a single class or interface. I believe that this implies that you cannot have more then 65535 … Read more

MVC 5.1 Razor DisplayFor not working with Enum DisplayName

Create new folder Views/Shared/DisplayTemplates Add empty Partial View named Enum, to the folder Replace Enum View code with: @model Enum @if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata)) { // Display Enum using same names (from [Display] attributes) as in editors string displayName = null; foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model)) { if (item.Selected) { displayName = item.Text ?? item.Value; } … Read more

How to define static constants in a Java enum?

As IntelliJ IDEA suggest when extracting constant – make static nested class. This approach works: @RequiredArgsConstructor public enum MyEnum { BAR1(Constants.BAR_VALUE), FOO(“Foo”), BAR2(Constants.BAR_VALUE), …, BARn(Constants.BAR_VALUE); @Getter private final String value; private static class Constants { public static final String BAR_VALUE = “BAR”; } }

C# numeric enum value as string

You should just be able to use the overloads of Enums ToString method to give it a format string, this will print out the value of the enum as a string. public static class Program { static void Main(string[] args) { var val = Urgency.High; Console.WriteLine(val.ToString(“D”)); } } public enum Urgency { VeryHigh = 1, … Read more

How to properly use the “choices” field option in Django

I think no one actually has answered to the first question: Why did they create those variables? Those variables aren’t strictly necessary. It’s true. You can perfectly do something like this: MONTH_CHOICES = ( (“JANUARY”, “January”), (“FEBRUARY”, “February”), (“MARCH”, “March”), # …. (“DECEMBER”, “December”), ) month = models.CharField(max_length=9, choices=MONTH_CHOICES, default=”JANUARY”) Why using variables is better? … Read more