I ahve a combo whose source is an Enum. Now , among the other values(say value1, value2
etc.) there is one item Changes(%)
that will be displayed in the combo .
How to define Changes(%) in the enum?
Using C#3.0
Thanks
I ahve a combo whose source is an Enum. Now , among the other values(say value1, value2
etc.) there is one item Changes(%)
that will be displayed in the combo .
How to define Changes(%) in the enum?
Using C#3.0
Thanks
You can't. Enum value names have to be valid C# identifiers. You shouldn't be trying to put display names in there.
Instead, consider decorating each value with a [Description]
attribute which you can fetch with reflection:
public enum ChangeType
{
[Description("Changes (%)")
PercentageChanges,
[Description("Changes (absolute)")]
AbsoluteChanges
}
Alternatively define resources, possibly using the enum value name as the resource key. This will be better for i18n purposes, apart from anything else, although more work if you don't need your app to be internationalized.
C# enumerations compile out as sealed classes, inheriting from Enum, with public static fields carrying the name of your enumeration members, thus, you're asking the compiler to name fields things like "<", ">" and "=", and that's not accepted.
Enumeration values carry the same restrictions as properties and fields when it comes to naming.
FWIW ... this article goes in-depth about the exact methodology of achieving what Jon Skeet suggests >>>> ... Pulling Enumeration Descriptions From A Resource File