Enum or Struct Help: Working with String Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chevron Boyde

    Enum or Struct Help: Working with String Values

    Hi There

    I have some codes that represent Sale Types i.e. A = On Account, C = Cash, D
    = Debtor, V = Voucher

    I want to create an enum or struct to work with the logical names like
    "Cash" as opposed to "C"

    I see the enums cannot work with string values like
    public enum SaleType
    {
    OnAccount = "A",
    Cash = "C"
    }

    Is there another way to do this, maybe with structs? Yet structs seem to
    need Constructors to set the public properties/public variables.

    Any tips appreciated,

    Chev

  • Anthony Jones

    #2
    Re: Enum or Struct Help: Working with String Values

    "Chevron Boyde" <samuel@buzzjui cebars.comwrote in message
    news:ewWalUmtIH A.1240@TK2MSFTN GP02.phx.gbl...
    Hi There
    >
    I have some codes that represent Sale Types i.e. A = On Account, C = Cash,
    D
    = Debtor, V = Voucher
    >
    I want to create an enum or struct to work with the logical names like
    "Cash" as opposed to "C"
    >
    I see the enums cannot work with string values like
    public enum SaleType
    {
    OnAccount = "A",
    Cash = "C"
    }
    >
    Is there another way to do this, maybe with structs? Yet structs seem to
    need Constructors to set the public properties/public variables.
    >
    Here is a simplistic solution:-

    public static class SaleType
    {
    public static readonly string OnAccount = "A";
    public static readonly string Cash = "C";
    }

    However you can't use this to type a variable as you would an enum

    IOW this is an error:-

    SaleType x = SaleType.Cash

    it would need to be:-

    string x = SaleType.Cash


    --
    Anthony Jones - MVP ASP/ASP.NET


    Comment

    • Dave Shooter

      #3
      Re: Enum or Struct Help: Working with String Values

      "Chevron Boyde" <samuel@buzzjui cebars.comwrote in message
      news:ewWalUmtIH A.1240@TK2MSFTN GP02.phx.gbl...
      Hi There
      >
      I have some codes that represent Sale Types i.e. A = On Account, C = Cash,
      D = Debtor, V = Voucher
      >
      I want to create an enum or struct to work with the logical names like
      "Cash" as opposed to "C"
      >
      I see the enums cannot work with string values like
      public enum SaleType
      {
      OnAccount = "A",
      Cash = "C"
      }
      >
      Is there another way to do this, maybe with structs? Yet structs seem to
      need Constructors to set the public properties/public variables.
      >
      Any tips appreciated,
      >
      Chev
      Maybe you could use the codes as the enum elements then use the
      [Description] attribute on the elements of the enum to get meaningful names:

      using System;
      using System.Componen tModel;

      public enum SaleType
      {
      [Description("On Account")]
      A,
      [Description("Ca sh")]
      C,
      [Description("De btor")]
      D,
      [Description("Vo ucher")]
      V
      }

      I have a class in my toolbox (which admittedly I 'found' somewhere) which
      can then return the description if one has been defined:

      using System;
      using System.Componen tModel;
      using System.Reflecti on;

      namespace MyCompanyUtils
      {
      public static class EnumInfo
      {
      public static string GetElementDescr iption(Enum value)
      {
      FieldInfo fi = value.GetType() .GetField(value .ToString());
      DescriptionAttr ibute[] attributes =
      (DescriptionAtt ribute[])fi.GetCustomAt tributes(
      typeof(Descript ionAttribute), false);
      return (attributes.Len gth 0) ? attributes[0].Description :
      value.ToString( );
      }
      }
      }

      class Program
      {
      static void Main(string[] args)
      {
      SaleType st = SaleType.A;
      Console.WriteLi ne(EnumInfo.Get ElementDescript ion(st));
      // Output: On Account
      }
      }


      Hope this helps.

      David


      Comment

      • Chevron Boyde

        #4
        Re: Enum or Struct Help: Working with String Values

        Thanks Tony!

        "Anthony Jones" <Ant@yadayadaya da.comwrote in message
        news:OztQKgmtIH A.4476@TK2MSFTN GP06.phx.gbl...
        "Chevron Boyde" <samuel@buzzjui cebars.comwrote in message
        news:ewWalUmtIH A.1240@TK2MSFTN GP02.phx.gbl...
        >Hi There
        >>
        >I have some codes that represent Sale Types i.e. A = On Account, C =
        >Cash,
        D
        >= Debtor, V = Voucher
        >>
        >I want to create an enum or struct to work with the logical names like
        >"Cash" as opposed to "C"
        >>
        >I see the enums cannot work with string values like
        >public enum SaleType
        >{
        > OnAccount = "A",
        > Cash = "C"
        >}
        >>
        >Is there another way to do this, maybe with structs? Yet structs seem to
        >need Constructors to set the public properties/public variables.
        >>
        >
        Here is a simplistic solution:-
        >
        public static class SaleType
        {
        public static readonly string OnAccount = "A";
        public static readonly string Cash = "C";
        }
        >
        However you can't use this to type a variable as you would an enum
        >
        IOW this is an error:-
        >
        SaleType x = SaleType.Cash
        >
        it would need to be:-
        >
        string x = SaleType.Cash
        >
        >
        --
        Anthony Jones - MVP ASP/ASP.NET
        >
        >

        Comment

        Working...