StrataFrame Forum

Accessing EnumDisplayValue programmatically

http://forum.strataframe.net/Topic18577.aspx

By Leonard P. - 8/19/2008

Hello,

Lets seay I have this enum type:

 public enum MyEnum
    {
        [EnumDisplayValue("This is Value 1")]
        value1 = 1,
        [EnumDisplayValue("This is Value 2")]
        value2 = 2
    }

Now consider this code:

   

MyEnum myEnum = MyEnum.value2;
MessageBox.Show(myEnum.ToString());

The messagebox displays "value2", but I would like to see EnumDisplayValue "This is Value 2".

Is there an easy way to get the value of that attribute? I have a feeling I need to use reflections to get it.

Thank you for your help.

By Greg McGuffey - 8/19/2008

Leonard,



This is what I use:



''' <summary>

''' Give an enum type and a value, return the display text for

''' that value. This will use the EnumDisplayValueAttribute if

''' the enum has one defined.

''' </summary>

''' <param name="enumType">type of enum</param>

''' <param name="value">value to lookup</param>

Public Shared Function GetEnumDisplayText(ByVal enumType As Type, ByVal value As Object) As String

  '-- Establish a return variable

  Dim desc As String = String.Empty



  Dim fi As FieldInfo = enumType.GetField(System.Enum.GetName(enumType, value))

  Dim descAttr As EnumDisplayValueAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(EnumDisplayValueAttribute)), EnumDisplayValueAttribute)

  If descAttr IsNot Nothing Then

    desc = descAttr.GetDisplayValue()

  Else

    desc = value.ToString()

  End If



  '-- Return the enum description/name

  Return desc

End Function

By Leonard P. - 8/19/2008

Thanks Greg!

This is pretty much what I did at first (only with Generic method and looping through fields and attributes using reflections) :

private static string GetDisplayValueType<T>(Enum customEnum) where T : struct
        {

            string retValue = string.Empty;
           
            foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {

                object value = field.GetValue(null);


                foreach (Attribute attrib in field.GetCustomAttributes(true))
                {

                    if (attrib is EnumDisplayValueAttribute)
                    {

                        EnumDisplayValueAttribute customAttr = (EnumDisplayValueAttribute)attrib;
                        if (customEnum.Equals(value))
                        {
                            retValue = customAttr.ValueOrKey;
                            break;
                        }
                    }

                }

            }

            return retValue;

        }

Using your code, I was able to get rid of loops:

     private static string GetDisplayValueType1<T>(Enum customEnum) where T : struct
        {

            string desc = string.Empty;
            FieldInfo field = typeof(T).GetField(Enum.GetName(typeof(T), customEnum));
            EnumDisplayValueAttribute descrAttr = (EnumDisplayValueAttribute)Attribute.GetCustomAttribute(field, typeof(EnumDisplayValueAttribute));
            if (descrAttr != null)
              desc = descrAttr.ValueOrKey;           
            else
               desc = customEnum.ToString();

           return desc;

        }

The second function looks much cleaner Smile Thanks!

By Greg McGuffey - 8/19/2008

Ooh, I like the generics....think I'll update my code to use it! Thanks back at ya! w00t
By Dustin Taylor - 8/19/2008

Good solutions! BigGrin

For future reference, there is also a built-in function in StrataFrame to do this very thing. MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(yourEnum).

By Greg McGuffey - 8/19/2008

Now he tells us.... Tongue
By Edhy Rijo - 8/19/2008

Dustin Taylor (08/19/2008)
Good solutions! BigGrin

For future reference, there is also a built-in function in StrataFrame to do this very thing. MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(yourEnum).

Hi Dustin,

In this very same topic, today I just hit an Enum Display problem in a Dialog Browser in which the enum is not properly displayed when selected in field for the browser result set as in this picture:

I guess the SF combobox is using the MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(EnumValue) but the field in the browser result is not.  I fixed my code by changing my DB class and populate the CustomerType field throug the RowPopulating event which works nice, but I wonder if the framework should use the GetEnumDisplayValue() every where an enum is used.

Here is my BD code:

Public Class CustomerBrowserDialog

     Private Sub CustomerBrowserDialog_RowPopulating(ByVal e As MicroFour.StrataFrame.UI.Windows.Forms.RowPopulatingEventArgs) Handles MyBase.RowPopulating

          '-- Strong type the passed business object

          With CType(e.BusinessObject, atr_crm.Business.bizCustomers)

          '-- Format the customer type enum to display properly.

          e.Values(4).DisplayValue = MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(.CustomerType)

          End With

     End Sub

End Class

BTW, thanks to Greg and Leonard, this post came up just on time with my situation, even though I used Dustin approach BigGrin.

By Trent L. Taylor - 8/20/2008

You could do all of that to get the EnumDisplayValue or...

MyEnum myEnum = MyEnum.value2;
MessageBox.Show(MicroFour.StrataFrame.Tools.Common.GetEnumDisplayValue(myEnum));

The framework already has a mechanism for you to do this...and it reduces the code by a lot! Wink

By Trent L. Taylor - 8/20/2008

LOL...I just saw that Dustin already mentioned it BigGrin
By Dustin Taylor - 8/20/2008

Honestly Trent, catch up BigGrin.