views:

63

answers:

3

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

+5  A: 

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.

EDIT: Here's an article going into more detail.

Jon Skeet
@priyanka: Well don't bind like that, basically. Binding directly to the values of an enum with no extra code and no template *will* show the value. I suggest you provide your combo box with an item template which fetches the relevant description.
Jon Skeet
@Jon Skeet I think the link I provided as supplementary-answer below has a good chance of visibility as a part of your accepted answer. http://www.4guysfromrolla.com/articles/080410-1.aspx. Request a edit+add..if you deem it worthy.
Shankar Ramachandran
@Shankar: Done.
Jon Skeet
A: 

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.

MUG4N
Yeah Jon gives you a workaround, I give you the reason.
MUG4N
A: 

FWIW ... this article goes in-depth about the exact methodology of achieving what Jon Skeet suggests >>>> ... Pulling Enumeration Descriptions From A Resource File

Shankar Ramachandran