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 737953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:46:52+00:00 2026-05-14T07:46:52+00:00

I have the following methods in an enum helper class (I have simplified it

  • 0

I have the following methods in an enum helper class (I have simplified it for the purpose of the question):

static class EnumHelper
{
    public enum EnumType1 : int
    {
        Unknown = 0,
        Yes = 1,
        No = 2
    }

    public enum EnumType2 : int
    {
        Unknown = 0,
        Dog = 1,
        Cat = 2,
        Bird = 3
    }

    public enum EnumType3
    {
        Unknown,
        iPhone,
        Andriod,
        WindowsPhone7,
        Palm
    }

    public static EnumType1 ConvertToEnumType1(string value)
    {
        return (string.IsNullOrEmpty(value)) ?
            EnumType1.Unknown :
            (EnumType1)(Enum.Parse(typeof(EnumType1), value, true));
    }

    public static EnumType2 ConvertToEnumType2(string value)
    {
        return (string.IsNullOrEmpty(value)) ?
            EnumType2.Unknown :
            (EnumType2)(Enum.Parse(typeof(EnumType2), value, true));
    }

    public static EnumType3 ConvertToEnumType3(string value)
    {
        return (string.IsNullOrEmpty(value)) ?
            EnumType3.Unknown :
            (EnumType3)(Enum.Parse(typeof(EnumType3), value, true));
    }
}

So the question here is, can I trim this down to an Enum extension method or maybe some type of single method that can handle any type. I have found some examples to do so with basic enums but the difference in my example is all the enums have the Unknown item that I need returned if the string is null or empty (if no match is found I want it to fail).

Looking for something like the following maybe:

EnumType1 value = EnumType1.Convert("Yes");
// or
EnumType1 value = EnumHelper.Convert(EnumType1, "Yes");

One function to do it all… how to handle the Unknown element is the part that I am hung up on.

Edit: Adjusted one of the enums to not be defined with integers. So I can guarantee that 0 will always be the case but Unknown will always be the correct text… I guess I could use the same example as the T(0) but do another parse on the text “Unknown”.

  • 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-14T07:46:52+00:00Added an answer on May 14, 2026 at 7:46 am

    Use this, assuming that Unknown is always the 0 value.

    public static T ConvertToEnum<T>(this string value) where T : new()
    {
        if( !typeof(T).IsEnum )
            throw new NotSupportedException( "T must be an Enum" );
    
        try
        {
            return (T)Enum.Parse(typeof(T), value);
        }
        catch
        {
            return default(T); // equivalent to (T)0
            //return (T)Enum.Parse(typeof(T), "Unknown"));
        }
    }
    

    Usage:

    EnumType2 a = "Cat".ConvertToEnum<EnumType2>(); 
    EnumType2 b = "Person".ConvertToEnum<EnumType2>(); // Unknown
    

    EDIT By OP (Kelsey): Your answer lead me to the correct answer so I thought I would include it here:

    public static T ConvertTo<T>(this string value)
    {
        T returnValue = (T)(Enum.Parse(typeof(T), "Unknown", true));
        if ((string.IsNullOrEmpty(value) == false) && 
            (typeof(T).IsEnum))
        {
            try { returnValue = (T)(Enum.Parse(typeof(T), value, true)); }
            catch { }
        }
        return returnValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {
I have something similar to the following method: public ActionResult Details(int id) { var
I have the following enum: [Flags] public enum PostAssociations { None = 0x0, User
I have the following structure: public enum MyTypes { Type1 = 1, Type2 =
Say I have the following methods: def methodA(arg, **kwargs): pass def methodB(arg, *args, **kwargs):
I have seen the following methods of putting JavaScript code in an <a> tag:
I have parsed XML using both of the following two methods... Parsing the XmlDocument
Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);
I have a Person and an Organisation Entity: Person looks like this: public class

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.