Accessing EnumDisplayValue programmatically


Author
Message
Leonard P.
Leonard P.
StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
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.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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


Leonard P.
Leonard P.
StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
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!

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Ooh, I like the generics....think I'll update my code to use it! Thanks back at ya! w00t
Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
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).

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Now he tells us.... Tongue
Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
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.



Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
LOL...I just saw that Dustin already mentioned it BigGrin
Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Honestly Trent, catch up BigGrin.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search