Can I construct an enum of enums and have a method switch the internal enum based on a variable
Date : March 29 2020, 07:55 AM
With these it helps This is how I ended up solving the issue. Each parameter to the enum represents separate environments, and I just use getter methods to return the correct id per environment. public enum EnvironmentIds {
USER_MANAGER_CHANGED("e29e902c","28a6e01c", "281916f6"),
USER_ADDED_TO_GROUP("5059020b", "f039fc37", "0e480029"),
USER_CREATED("d9bc6a0c", "f26f0a23", "9261f53a"),
USER_REMOVED_FROM_GROUP("fc30b97c", "15528295", "c08af3b2");
String holdingId;
String productionId;
String stagingId;
EnvironmentIds(String holdingId, String stagingId, String productionId) {
this.holdingId = holdingId;
this.stagingId = stagingId;
this.productionId = productionId;
}
public String getId(String environment) {
switch(environment) {
case "holding":
return getHoldingId();
case "production":
return getProductionId();
case "staging":
return getStagingId();
default:
return null;
}
}
public String getHoldingId() {return holdingId;}
public String getProductionId() {return productionId;}
public String getStagingId() {return stagingId;}
}
|
Is there any significant reason to use the ordinal of an enum in a switch case, instead of using the enum?
Date : March 29 2020, 07:55 AM
I wish did fix the issue. There's a good reason against it. The ordinals can change any time without breaking even binary compatibility. The names cannot so change at all. A better solution would have been to build the returned values into the Enum itself.
|
Java forcing default return statement from method with enum switch with all enum values covered
Tag : java , By : Dennizzz
Date : March 29 2020, 07:55 AM
Hope that helps Java forcing default return statement from method with enum switch with all enum values covered... What is the design decision behind such behaviour? case ...:
return ...;
default:
// here in case someone updates the enum and forgets to update this code
return "Unknown type";
case ...:
return ...;
default:
throw new IllegalStateException("unknown enum type found for" + mType);
|
Is it possible to give out a warning when switch (enum) does not cover every enum value AT COMPILE C#
Tag : chash , By : tanminivan
Date : March 29 2020, 07:55 AM
wish of those help No, it's not possible - at least not out of the box. Switch statements in C# do not have to be exhaustive, and there's no way to require them to be exhaustive. (Switch expressions in C# 8 will give a warning if they're not exhaustive, but that probably doesn't help you right now.) <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Roslyn.Analyzers" Version="1.0.3.4" PrivateAssets="All" />
</ItemGroup>
</Project>
using System;
enum Color
{
Red, Green, Blue
}
sealed class Program
{
static void Main(string[] args)
{
Color c = Color.Red;
switch (c)
{
case Color.Red:
Console.WriteLine("Red");
break;
case Color.Green:
Console.WriteLine("Green");
break;
// case Color.Blue:
// Console.WriteLine("Blue");
// break;
default:
Console.WriteLine("Undefined color");
break;
}
}
}
|
Using attributes to cut down on enum to enum mapping and enum/const to action switch statments
Date : March 29 2020, 07:55 AM
|