"Chevron Boyde" <sa****@buzzjuicebars.comwrote in message
news:ew**************@TK2MSFTNGP02.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.ComponentModel;
public enum SaleType
{
[Description("On Account")]
A,
[Description("Cash")]
C,
[Description("Debtor")]
D,
[Description("Voucher")]
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.ComponentModel;
using System.Reflection;
namespace MyCompanyUtils
{
public static class EnumInfo
{
public static string GetElementDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length 0) ? attributes[0].Description :
value.ToString();
}
}
}
class Program
{
static void Main(string[] args)
{
SaleType st = SaleType.A;
Console.WriteLine(EnumInfo.GetElementDescription(s t));
// Output: On Account
}
}
Hope this helps.
David