Click here to Skip to main content
15,907,910 members
Home / Discussions / C#
   

C#

 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming20-Apr-20 9:34
mveRichard Deeming20-Apr-20 9:34 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 11:56
professionalBillWoodruff20-Apr-20 11:56 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 22:10
professionalBillWoodruff20-Apr-20 22:10 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 0:01
mveRichard Deeming21-Apr-20 0:01 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 4:30
professionalBillWoodruff21-Apr-20 4:30 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 5:17
mveRichard Deeming21-Apr-20 5:17 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:29
professionalBillWoodruff21-Apr-20 8:29 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 5:36
sysadminMatthew Dennis21-Apr-20 5:36 
This might be simpler
C#
using System;

namespace EnumConvert
{
    public enum MyEnum
    {
        None = 0,
        First,
        Second,
        Third,
        Last
    }

    public static class EnumExtensions
    {
        public static Tenum ToEnum<Tenum>(this Tenum theEnum, string str, bool ignoreCase = false)
            where Tenum : struct, Enum
        {
            if (Enum.TryParse<Tenum>(str, ignoreCase, out Tenum result))
                return result;

            throw new ArgumentException($"{str} is not a valid string for conversion to {typeof(Tenum).FullName}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyEnum myEnum = new MyEnum();

            var none = myEnum.ToEnum("None");
            Console.WriteLine($"'None' results in {none} with a value of {(int)none}");

            var first = myEnum.ToEnum("First");
            Console.WriteLine($"'First' results in {first} with a value of {(int)first}");

            var last = myEnum.ToEnum("last", true);
            Console.WriteLine($"'last' results in {last} with a value of {(int)last}");

            try
            {
                last = myEnum.ToEnum("last", false);
                Console.WriteLine($"'last' results in {last} with a value of {(int)last}");
            }
            catch
            {
                Console.WriteLine("Error trying to convert 'last' to MyEnum");
            }

            try
            {
                var random = myEnum.ToEnum("Random", true);
                Console.WriteLine($"'Random' results in {random} with a value of {(int)random}");
            }
            catch
            {
                Console.WriteLine("Error trying to convert 'random' to MyEnum");
            }

        }
    }
}
"Time flies like an arrow. Fruit flies like a banana."


modified 21-Apr-20 12:19pm.

GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:27
professionalBillWoodruff21-Apr-20 8:27 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 8:37
mveRichard Deeming21-Apr-20 8:37 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:59
professionalBillWoodruff21-Apr-20 8:59 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 9:16
sysadminMatthew Dennis21-Apr-20 9:16 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 20:57
professionalBillWoodruff21-Apr-20 20:57 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis22-Apr-20 4:35
sysadminMatthew Dennis22-Apr-20 4:35 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 6:12
professionalBillWoodruff22-Apr-20 6:12 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 21:53
professionalBillWoodruff21-Apr-20 21:53 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 23:25
mveRichard Deeming21-Apr-20 23:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 3:01
professionalBillWoodruff22-Apr-20 3:01 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 7:25
professional#realJSOP22-Apr-20 7:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 7:39
professionalBillWoodruff22-Apr-20 7:39 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 7:47
professional#realJSOP22-Apr-20 7:47 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 20:31
professionalBillWoodruff22-Apr-20 20:31 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP23-Apr-20 0:58
professional#realJSOP23-Apr-20 0:58 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff23-Apr-20 5:25
professionalBillWoodruff23-Apr-20 5:25 
QuestionSas.dll Pin
Member 1480415820-Apr-20 4:26
Member 1480415820-Apr-20 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.