Problem
Because the Enumeration’s type must be an int, I know the following isn’t possible.
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
I got a field with incomprehensible codes from my database (the OEM and CMBs). I’d like to turn this field into an enum or something else more comprehensible. Because if readability is the goal, the answer should be brief.
Is there anything else I can do?
Asked by Boris Callens
Solution #1
I prefer to utilize properties in a class over methods because they have a more enum-like appearance.
An example of a Logger is as follows:
public class LogCategory
{
private LogCategory(string value) { Value = value; }
public string Value { get; private set; }
public static LogCategory Trace { get { return new LogCategory("Trace"); } }
public static LogCategory Debug { get { return new LogCategory("Debug"); } }
public static LogCategory Info { get { return new LogCategory("Info"); } }
public static LogCategory Warning { get { return new LogCategory("Warning"); } }
public static LogCategory Error { get { return new LogCategory("Error"); } }
}
As a parameter, pass in type-safe string values:
public static void Write(string message, LogCategory logCategory)
{
var log = new LogEntry { Message = message };
Logger.Write(log, logCategory.Value);
}
Usage:
Logger.Write("This is almost like an enum.", LogCategory.Info);
Answered by Even Mien
Solution #2
You might also utilise the extension model, which is as follows:
public enum MyEnum
{
[Description("String 1")]
V1= 1,
[Description("String 2")]
V2= 2
}
Your Extension Class
public static class MyEnumExtensions
{
public static string ToDescriptionString(this MyEnum val)
{
DescriptionAttribute[] attributes = (DescriptionAttribute[])val
.GetType()
.GetField(val.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
usage:
MyEnum myLocal = MyEnum.V1;
print(myLocal.ToDescriptionString());
Answered by Glennular
Solution #3
Why not use a static class that has constants?
static class GroupTypes
{
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
void DoSomething(string groupType)
{
if(groupType == GroupTypes.TheGroup)
{
// Be nice
}
else if (groupType == GroupTypes.TheOtherGroup)
{
// Continue to be nice
}
else
{
// unexpected, throw exception?
}
}
Answered by 3 revs
Solution #4
Adding constants to a static class is a good idea. You won’t get a Type, but you will get a set of understandable, well-organized constants:
public static class GroupTypes {
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
Answered by darasd
Solution #5
It’s actually quite simple to do. Use the code below to get started.
enum GroupTypes
{
OEM,
CMB
};
Then just use the following line of code to get the string value of each enum element.
String oemString = Enum.GetName(typeof(GroupTypes), GroupTypes.OEM);
I’ve successfully used this way in the past, as well as a constants class to keep string constants; both work well, but I prefer this method.
Answered by Arthur C
Post is based on https://stackoverflow.com/questions/630803/associating-enums-with-strings-in-c-sharp