Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

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!
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

It probably should, but I can not figure it out.  I just need to convert from string to enum.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);

Open in new window

Sample Code
using 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)
            {
            }

        }
    }
}

Open in new window