Geeks With Blogs
Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.
improve my => 'code'

Before I posted a method that converts from one enumeration to another by value...  Here's one that does the conversion by string (by the enumeration name)... 

    public static class EnumHelper

    {

        public static int ConvertEnumToEnumIntByString<T, U>(U enumArg)

        {

            string [] arrNames = Enum.GetNames(typeof(T));

            int[] arrValues = (int[] ) Enum.GetValues(typeof(T));

            for (int counter = 0; counter < arrNames.Length; counter++)

            {

                if (arrNames[counter] == enumArg.ToString())

                {

                    return  arrValues[counter];               

                }

            }

            throw new ApplicationException("Enum conversion did not work.");

        }  

    }

         private void testNewConversion()

        {

            Enum1 testInput = Enum1.P1;

            Enum2 testOutput = (Enum2) EnumHelper.ConvertEnumToEnumIntByString

                < Enum2, Enum1> (testInput);

        }

 

Interested in your comments,
Jonathan

Posted on Tuesday, October 23, 2007 4:22 PM C# | Back to top


Comments on this post: Another Enumeration Helper Function

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jonathan Starr | Powered by: GeeksWithBlogs.net