Hi,

This is not exactly what you asked for, but the closest I know of is to
apply an attribute (System.ComponentModel.DescriptionAttribute works well)
to each enum value and use Reflection to retrieve the value, e.g.:

    public enum MonetaryUnit
    {
      [Description("American")]
      USDollar,
      //...
    }

    public class DescriptionHelper
    {
        static public string GetDescription(Enum e)
        {
            string val = e.ToString();
            Type t = e.GetType();
            FieldInfo fi = t.GetField(val);
            DescriptionAttribute[] att = (DescriptionAttribute[])
                fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (att.Length > 0) return att[0].Description;
            else return val;
        }
    }

-Gert

-----Original Message-----
From: Clark, Michael (OFM) [mailto:[EMAIL PROTECTED]
Sent: Monday, June 23, 2003 11:56 AM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] ToString Implementation in an Enum


There's gotta be a way, but I can't find it...

Suppose you have an enum, such as:

public enum MonetaryUnit
{
        USDollar,
        AUDollar,
        CDNDollar
}

And you would like the expression MonetaryUnit.USDollar.ToString() to return
something like "American", or AUDollar to return "Australian", etc.

How would you go about implementing this?

Reply via email to