Posts
74
Comments
75
Trackbacks
0
Another Enumeration Helper Function
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 Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 1 and 6 and type the answer here:
News
Jonathan Starr is a developer in Saint Louis, MO. He holds an MBA in Finance from Columbia Business School and earned his MCSD from Microsoft.


All statements in this blog are personal opinions and do not reflect the opinions of his employer.





Related Sites
Join My Community at MyBloglog!

Tag Cloud