Dynamically assign enumeration selection from variable

Hi, I'm pretty new at using enumerators.  I would set the value of the enumerator based on a value set in a database table.  Below is a simplified attempt to show what I'm trying to do ... I'm open to any methods to do this, doesn't have to be a switch.  I am also getting an error on the 2nd RouteMethod saying: a local variable name 'RouteMethod' is already defined in this scope.

static string method1()

//value from database
String RM = dbConfig("RouteMethod").ToString();
            switch (RM)
            {
                case "Practical":
                    RouteType RouteMethod = RouteType.Practical;
                    break;
                case "Shortest":
                    RouteType RouteMethod = RouteType.Shortest;
                    break;
                case "National":
                    RouteType RouteMethod = RouteType.National;
                    break;
                case "FiftyThree":
                    RouteType RouteMethod = RouteType.FiftyThree;
                    break;
                case "AvoidToll":
                    RouteType RouteMethod = RouteType.AvoidToll;
                    break;
                case "Air":
                    RouteType RouteMethod = RouteType.Air;
                    break;
                default:
                    RouteType RouteMethod = RouteType.Practical;
                    break;
            }   

Open in new window

Scarlett72Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
Your enum declaration is missing from the example but I'm assuming it's something like this:

public Enum RouteType
{
  Practical,
  Shortest,
  National,
  FiftyThree,
  AvoidToll,
  Air
}

Open in new window


If that's the case then they have integer values starting with zero. Store your value in your database as an int and do a simple cast on the way out.

RouteType RouteMethod = (RouteType)Convert.ToInt32(dbConfig("RouteMethod"))

Open in new window


If you really want to store as a string (which does make it easier to read in the database you could cast it like this:

(RouteType)Enum.Parse(typeof(RouteType), dbConfig("RouteMethod").toString());

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scarlett72Author Commented:
Thank you Russ that worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.