Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Enums and text descriptions

Enums and text descriptions

Scheduled Pinned Locked Moved C#
tutorial
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J J4amieC

    The description attribute is exactly what I personally use for this, with a simple helper method to obtain the description from the enum item:

    public static class EnumHelper
    {
        public static string GetDescription(System.Enum enumValue)
        {
            FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
            object[] descAttribs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (descAttribs.Length == 0)
                return null;
            return ((DescriptionAttribute)descAttribs[0]).Description;
        }
    }
    

    Usage: string description = EnumHelper.GetDescription(MyEnum.Value1); Hope that helps.

    M Offline
    M Offline
    MrEyes
    wrote on last edited by
    #4

    Thanks alot, that works perfectly Out of curiosity, is it possible to add object type attributes? for example

    public enum EnumAlpha
    {
    value1,
    value2
    }

    public enum EnumBeta
    {
    [ObjectDescription(EnumAlpha.value1)]
    value1,
    [ObjectDescription(EnumAlpha.value2)]
    value2,
    }

    J P 2 Replies Last reply
    0
    • M MrEyes

      Thanks alot, that works perfectly Out of curiosity, is it possible to add object type attributes? for example

      public enum EnumAlpha
      {
      value1,
      value2
      }

      public enum EnumBeta
      {
      [ObjectDescription(EnumAlpha.value1)]
      value1,
      [ObjectDescription(EnumAlpha.value2)]
      value2,
      }

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #5

      I dont see what you're trying to achieve, but the answer is yes, you can define your own attributes and markup your code with those attributes.

      M 1 Reply Last reply
      0
      • J J4amieC

        I dont see what you're trying to achieve, but the answer is yes, you can define your own attributes and markup your code with those attributes.

        M Offline
        M Offline
        MrEyes
        wrote on last edited by
        #6

        Well to cut a very long story short I am currently in the throws of creating an XML data transformation library. Unfortunately for me neither XML structure can be schema'd and these are also out of my control and cannot be change. To get around this I have created a series of enums for the 100's of different keys these items contain, so now I am trying to create mapping data from one to the other. It could be done with switch/case statements, however this is messy and will be complicated to maintain. So I am attempting to use attributes on the input enums to easily convert these to the output format. So for example each data structure contains the concept of Address Type, e.g.

        public enum OutputAddressType
        {
        [Description("ResidentialAddress")]
        Residential,
        [Description("CommercialAddress")]
        Commericial,
        [Description("UnknownAddress")]
        Unknown,

        }

        public enum InputAddressType
        {
        [Description("ADR001")]
        Home,
        [Description("ADR002")]
        HomeSecondary,
        [Description("ADR003")]
        Business
        }

        Now as I iterate through the input data object address values, I currently have a switch case that looks something like this:

        switch(input.AddressType)
        {
        case InputAddressType.Home:
        this.output.AddressType = OutputAddressType.ResidentialAddress;
        break;
        case InputAddressType.HomeSecondary:
        this.output.AddressType = OutputAddressType.Unknown;
        break;
        case InputAddressType.Business:
        this.output.AddressType = OutputAddressType.CommercialAddress;
        break;
        }

        It would be great if I could do something like this to the input enum:

        public enum InputAddressType
        {
        [ObjectDescription(InputAddressType.ResidentialAddress)]
        [Description("ADR001")]
        Home,
        [ObjectDescription(InputAddressType.Unknown)]
        [Description("ADR002")]
        HomeSecondary,
        [ObjectDescription(InputAddressType.CommercialAddress)]
        [Description("ADR003")]
        Business
        }

        The example above is a simple example, however if you imagine that there are over 200 of these enums, each having anywhere from 2 to 200 values you can see why having loads of switch/cases is unmaintainable. Also in the example there is a direct 1 to 1 map from input to output, however in the real world this is a rarity.

        P 1 Reply Last reply
        0
        • M MrEyes

          Thanks alot, that works perfectly Out of curiosity, is it possible to add object type attributes? for example

          public enum EnumAlpha
          {
          value1,
          value2
          }

          public enum EnumBeta
          {
          [ObjectDescription(EnumAlpha.value1)]
          value1,
          [ObjectDescription(EnumAlpha.value2)]
          value2,
          }

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          There are limitations on what types of values may be passed to attributes. Generally, only strings and numerics. Even enum values have to be cast to their underlying type (int, short, etc.).

          1 Reply Last reply
          0
          • J J4amieC

            The description attribute is exactly what I personally use for this, with a simple helper method to obtain the description from the enum item:

            public static class EnumHelper
            {
                public static string GetDescription(System.Enum enumValue)
                {
                    FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
                    object[] descAttribs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (descAttribs.Length == 0)
                        return null;
                    return ((DescriptionAttribute)descAttribs[0]).Description;
                }
            }
            

            Usage: string description = EnumHelper.GetDescription(MyEnum.Value1); Hope that helps.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #8

            But you're using Reflection on each call which is very slow, better to read them once and cache the results (after all the values aren't changing).

            J 1 Reply Last reply
            0
            • M MrEyes

              I have a series of fairly large enums, and each value within them has a free text equivalent. Within my code, I need to get the free text for a given enum value. Currently I am using a fairly traditional method of doing this, i.e. a switch/case on the enum value which obtains the text description. However this is proving to be a pain to maintain, so does anybody know of a way of adding the free text as an attribute of the enum. For example

              public enum MyEnum
              {
              [Description("This Wibbles")]
              Value1,
              [Description("This Wobbles")]
              Value2,
              [Description("Ding Dong")]
              Value3
              }

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #9

              You might find this[^] helpful.

              1 Reply Last reply
              0
              • M MrEyes

                Well to cut a very long story short I am currently in the throws of creating an XML data transformation library. Unfortunately for me neither XML structure can be schema'd and these are also out of my control and cannot be change. To get around this I have created a series of enums for the 100's of different keys these items contain, so now I am trying to create mapping data from one to the other. It could be done with switch/case statements, however this is messy and will be complicated to maintain. So I am attempting to use attributes on the input enums to easily convert these to the output format. So for example each data structure contains the concept of Address Type, e.g.

                public enum OutputAddressType
                {
                [Description("ResidentialAddress")]
                Residential,
                [Description("CommercialAddress")]
                Commericial,
                [Description("UnknownAddress")]
                Unknown,

                }

                public enum InputAddressType
                {
                [Description("ADR001")]
                Home,
                [Description("ADR002")]
                HomeSecondary,
                [Description("ADR003")]
                Business
                }

                Now as I iterate through the input data object address values, I currently have a switch case that looks something like this:

                switch(input.AddressType)
                {
                case InputAddressType.Home:
                this.output.AddressType = OutputAddressType.ResidentialAddress;
                break;
                case InputAddressType.HomeSecondary:
                this.output.AddressType = OutputAddressType.Unknown;
                break;
                case InputAddressType.Business:
                this.output.AddressType = OutputAddressType.CommercialAddress;
                break;
                }

                It would be great if I could do something like this to the input enum:

                public enum InputAddressType
                {
                [ObjectDescription(InputAddressType.ResidentialAddress)]
                [Description("ADR001")]
                Home,
                [ObjectDescription(InputAddressType.Unknown)]
                [Description("ADR002")]
                HomeSecondary,
                [ObjectDescription(InputAddressType.CommercialAddress)]
                [Description("ADR003")]
                Business
                }

                The example above is a simple example, however if you imagine that there are over 200 of these enums, each having anywhere from 2 to 200 values you can see why having loads of switch/cases is unmaintainable. Also in the example there is a direct 1 to 1 map from input to output, however in the real world this is a rarity.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #10

                Will XSLT work?

                M 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Will XSLT work?

                  M Offline
                  M Offline
                  MrEyes
                  wrote on last edited by
                  #11

                  Initially I did consider XSLT, however the input XML is a rather interesting example of XML usage, so creating the XSL transform would have been extremely convoluted. As an example the input XML is entirely attribute based and attribute values and presence depend on the value and presence of other attributes. So for example these two samples represent two different people (customer, staff)

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    But you're using Reflection on each call which is very slow, better to read them once and cache the results (after all the values aren't changing).

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #12

                    erm.... ok. Feel free to provide a sample that reads Description attributes without the use of Reflection. And I mean, come on, how slow is this method on the larger scheme of things? An extra 2 milliseconds? I dont think my users will mind.

                    P 1 Reply Last reply
                    0
                    • J J4amieC

                      erm.... ok. Feel free to provide a sample that reads Description attributes without the use of Reflection. And I mean, come on, how slow is this method on the larger scheme of things? An extra 2 milliseconds? I dont think my users will mind.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #13

                      J4amieC wrote:

                      An extra 2 milliseconds?

                      Obviously you haven't read this[^] yet.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups