Getting Enumeration Value using the [Description] Text

Hi
I know the GetDescription Function (in attached code) will return me the Description value from the Enum MyEnum

My problem is I want to retrieve Enum Value based on the Description Text I pass in.
ie, if I pass "Myenum 1" the function should return me Enum1
if I pass "Myenum 2", it should return me Enum2.

I tried to do that using Function GetEnumValueFromDescription

but it throws an exception on
line  ->   foreach (Enum e in Enum.GetValues(MyEnum.GetType()))
saying it expects System.Type

please help,

Thanks
Deepak
public enum MyEnum 
{ 
[Description ("Myenum 1")] Enum1, 
[Description ("Myenum 2")] Enum2, 
[Description ("Myenum 3")] Enum3
}
 
        public static string GetDescription<T>(T EnumValue)
        {
            FieldInfo fi = EnumValue.GetType().GetField(EnumValue.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : EnumValue.ToString();
        }
 
        public static Enum GetEnumValueFromDescription<T>(T MyEnum, string Description)
        {
            Enum retEnumValue = null;
 
            foreach (Enum e in Enum.GetValues(MyEnum.GetType()))
            {
                string sValue = GetDescription(MyEnum.GetType());
                if (sValue.ToLower() == Description.ToLower())
                {
                    retEnumValue = e;
                    break;
                }
            }
 
            return retEnumValue;
        }

Open in new window

DPalkarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WilliamCommented:
try

foreach ( Enum e in Enum.GetValues( typeof(  MyEnum) ) )
0
DPalkarAuthor Commented:
Thanks for your reply

I used the typeof(MyEnum) before and I received the same error so I tried the GetType.
I think that's what I remember. Currently I am home, I'll try that out again tomorrow and get back to you.

thanks
Deepak
0
DPalkarAuthor Commented:
Hi Billy,

When I changed it to typeof(MyEnum), I get following compile time error.

The type or namespace name 'MyEnum' could not be found (are you missing a using directive or an assembly reference?)

thanks
Deepak
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

WilliamCommented:
I think this is because the compiler is confussed....
You have MyEnum as the public enum MyEnum and then in the constructor for the method you have (t MyEnum ...)

Can you add the namespace to this as well?
like...
typeof(DPalkar.MyEnum),

Does this class have a namespace?

0
DPalkarAuthor Commented:
Yes
it has a namespace but it's in the other library.
but if I hardcode it like DPalkar.MyEnum on that line, it won't be generalized, because I wanted to use the same function for any Enum.

Is there any other to do this may be through Reflection?
thanks
Deepak
0
WilliamCommented:
whay not 'add' the type to the overload so....

 public static Enum GetEnumValueFromDescription<T>( T MyEnum , Type Type_  , string Description )
        {
            Enum retEnumValue = null;
 
            foreach ( Enum e in Enum.GetValues( Type_ ) )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DPalkarAuthor Commented:
Great!!
In fact, I didn't even need the "T" either.
        public static Enum GetEnumValueFromDescription(Type MyType, string Description)
just the Type keyword did the trick.

thanks a lot and have a great Day!
Deepak
        public static Enum GetEnumValueFromDescription(Type MyType, string Description)
        {
            Enum retEnumValue = null;
 
            foreach (Enum e in Enum.GetValues(MyType))
            {
                string sValue = GetDescription((Enum)e);
                if (sValue.ToLower() == Description.ToLower())
                {
                    retEnumValue = e;
                    break;
                }
            }
 
            return retEnumValue;
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.