?
Solved

Getting Enumeration Value using the [Description] Text

Posted on 2008-11-18
7
Medium Priority
?
2,638 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:DPalkar
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:William
ID: 22988551
try

foreach ( Enum e in Enum.GetValues( typeof(  MyEnum) ) )
0
 

Author Comment

by:DPalkar
ID: 22990656
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
 

Author Comment

by:DPalkar
ID: 22994117
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 11

Expert Comment

by:William
ID: 22996232
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
 

Author Comment

by:DPalkar
ID: 22996378
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
 
LVL 11

Accepted Solution

by:
William earned 200 total points
ID: 22996404
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
 

Author Comment

by:DPalkar
ID: 22996677
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question