curiouswebster
asked on
C#: Need to turn a text value into an enumeration
I have an enumeration and need to convert a string value into that matching enumerated value.
For example:
public enum RuleTypes
{
Wf_Adm = 1,
Wf_Case = 2,
Wf_Ref = 3,
}
If I have a string of "Wf_Case" I need to get a value of RuleTypes.Wf_Case.
I do not want to use a switch statement.
Thanks!
For example:
public enum RuleTypes
{
Wf_Adm = 1,
Wf_Case = 2,
Wf_Ref = 3,
}
If I have a string of "Wf_Case" I need to get a value of RuleTypes.Wf_Case.
I do not want to use a switch statement.
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Then to get your enum from the string you simply call: GetEnum("Wf_Case")
Np, good luck :)
RuleTypes rt = (RuleTypes)Enum.Parse(typeof(RuleTypes), typeString);
Sample Codeusing System;
enum RuleTypes { Wf_Adm = 1, Wf_Case = 2, Wf_Ref = 3};
namespace csharpee1
{
class Program
{
static void Main(string[] args)
{
string typeString = "Wf_Case";
try
{
RuleTypes rt = (RuleTypes)Enum.Parse(typeof(RuleTypes), typeString);
}
catch (ArgumentException)
{
}
}
}
}
ASKER