I'm trying to get a enum value from a string in a database, below is the enum I am trying to get the value from, the DB value is a string.
internal enum FieldType
{
Star,
Ghost,
Monster,
Trial,
None
}
Heres the DB part:
using (var reader = dbConnection.ExecuteReader())
{
var field = new Field(
reader.GetString("field_type"),
reader.GetString("data"),
reader.GetString("message")
);
}
Now, I had this method that did it for me, but it seems overkill to have a method do something that C# could already do, can anyone tell me if theres a way I can do this within the C# language without creating my own method?
public static FieldType GetType(string Type)
{
switch (Type.ToLower())
{
case "star":
return FieldType.Star;
case "ghost":
return FieldType.Ghost;
case "monster":
return FieldType.Monster;
case "trial":
return FieldType.Trial;
default:
case "none":
return FieldType.None;
}
}