Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:14:36+00:00 2026-05-23T21:14:36+00:00

Bit of a strange one this. Please forgive the semi-pseudo code below. I have

  • 0

Bit of a strange one this. Please forgive the semi-pseudo code below. I have a list of enumerated values. Let’s say for instance, like so:

public enum Types
    {
       foo = 1,
       bar = 2,
       baz = 3
    }

Which would become, respectfully, in the code:

Types.foo
Types.bar
Types.baz

Now I have a drop down list that contains the following List Items:

var li1 = new ListItem() { Key = "foo" Value = "Actual Representation of Foo" }
var li2 = new ListItem() { Key = "bar" Value = "Actual Representation of Bar" }
var li3 = new ListItem() { Key = "baz" Value = "Actual Representation of Baz" }

for the sake of completeness:

dropDownListId.Items.Add(li1); dropDownListId.Items.Add(li2); dropDownListId.Items.Add(li3);

Hope that everyone is still with me. What I want to do is to on the Autopostback is take the string “foo” and convert that to Types.foo – without using a switch (as the enumerated values are generated from a database and may change).

I hope that makes sense? Any idea where to even start?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:14:37+00:00Added an answer on May 23, 2026 at 9:14 pm

    Sure:

    Types t;
    if(Enum.TryParse(yourString, out t)) // yourString is "foo", for example
    {
        // use t
    }
    else
    {
        // yourString does not contain a valid Types value
    }
    

    There’s also an overload that takes a boolean that allows you to specify case insensitiveness:
    http://msdn.microsoft.com/en-us/library/dd991317.aspx

    Enum.TryParse is new in .NET 4. If you’re stuck on a previous version, you’ll have to use the non-typesafe Enum.Parse method (which throws an exception in case of conversion failure, instead of returning false), like so:

    try
    {
        Types t = (Types)Enum.Parse(typeof(Types), yourString);
        // use t
    }
    catch(ArgumentException)
    {
        // yourString does not contain a valid Types value
    }
    

    Enum.Parse also has an overload for case insensitiveness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This a bit of strange one.... We have an internal web app that runs
This one may be a bit strange. I have a solution in Visual Studio
This is a bit of a strange one, but I've been struggling for a
I know this is bit of a strange one but if anyone had any
This is a bit of a strange one but I've just seen something on
I realize this one is a bit strange, so I'll explain. For a simple
Bit of a strange one here, i have a CCTV system and contacted the
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I have a bit of a strange question. Can anyone tell me where to
I have a bit of a strange problem. I am trying to send in-app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.