Spaces in Enum

L

Luigi

Hi all,
is it possible to have spaces in enum values?
I have an enum like this:

public enum Columns
{ Italy = 1, OtherCountryUE = 2, OtherCountryOCSE = 3, OtherCountr=
4 }

and I'd like to have

public enum Columns
{ Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
Countr= 4 }

but this form is not valid.

How can I solve?

Thanks a lot.
 
J

Jon Skeet [C# MVP]

is it possible to have spaces in enum values?

No. The names have to be valid C# identifiers. You may wish to provide
an attribute for each value, if you want a human-readable form.

Jon
 
H

Hans Kesting

Luigi laid this down on his screen :
Hi all,
is it possible to have spaces in enum values?
I have an enum like this:

public enum Columns
{ Italy = 1, OtherCountryUE = 2, OtherCountryOCSE = 3, OtherCountr=
4 }

and I'd like to have

public enum Columns
{ Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
Countr= 4 }

but this form is not valid.

How can I solve?

Thanks a lot.

You can't. The enum-values have to be identifiers, so you are bound by
the naming rules for identifiers. And that means: no spaces.

Why do you want it?

Hans Kesting
 
L

Luigi

Thanks Jon and Hans.
The reason is that I have to put these Enum as caption of XtraTreeList of
DevExpress Suite.

L
 
G

G.S.

No. The names have to be valid C# identifiers. You may wish to provide
an attribute for each value, if you want a human-readable form.

Jon

I assume it's for display purposes (populate drop-down or similar).

I have done it quick-and-dirty like that:

//This is my enum
public enum Status
{
Not_selected = 0,
Pending = 1,
Complete = 2,
Re__Notification_Required //NOTE: __ is later replaced with - for
display purposes; _ is replaced with a space
}
//this is a helper that returns a dictionary ~~~: Make itgeneric
public static Dictionary<int, string> GetCaseFlowStatusSelection()
{
Dictionary<int, string> ret = new Dictionary<int, string>();
foreach (Status status in Enum.GetValues(typeof(Status)))
ret.Add((int)status, status.ToString().Replace("__",
"-").Replace("_", " "));
return ret;
}
//consum the solution :)
this.cmbCurrentStatus.DataSource = new
BindingSource(MyClass.GetCaseFlowStatusSelection(), null);
this.cmbCurrentStatus.DisplayMember = "Value";
this.cmbCurrentStatus.ValueMember = "Key";


Jon, if easy, could you post an example of the attribute solution.
 
J

Jon Skeet [C# MVP]

Thanks Jon and Hans.
The reason is that I have to put these Enum as caption of XtraTreeList of
DevExpress Suite.

Does XtraTreeList allow you any way of converting from a value to the
displayed item? (I'd hope so, for internationalisation purposes if
nothing else.)

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi all,
is it possible to have spaces in enum values?
I have an enum like this:

public enum Columns
{ Italy = 1, OtherCountryUE = 2, OtherCountryOCSE = 3, OtherCountr=
4 }

and I'd like to have

public enum Columns
{ Italy = 1, Other Country UE = 2, Other Country OCSE = 3, Other
Countr= 4 }

but this form is not valid.

How can I solve?

Thanks a lot.

Hi,

Well, It's pretty obvious why not. What you can do is using a _ but
IIRC MS guidelines do not recommend this. It does prefer OtherContryUE
format
 
I

Ignacio Machin ( .NET/ C# MVP )

Thanks Jon and Hans.
The reason is that I have to put these Enum as caption of XtraTreeList of
DevExpress Suite.

L

Hi,

You can have a method that transform the enum for you. Not only that
but you could want it in different languages for example.
 
L

Luigi

Jon Skeet said:
Does XtraTreeList allow you any way of converting from a value to the
displayed item? (I'd hope so, for internationalisation purposes if
nothing else.)

I think so, I have to check on its documentatio.

L
 
D

Duggi

Thanks Jon and Hans.
The reason is that I have to put these Enum as caption of XtraTreeList of
DevExpress Suite.

L

I think you can achieve with the displayName / tag / value
combinations. The values being enumerated, display name for DevExpress
Tree item can be set to a different value. And Tag is always there to
help. However you need to set tag value to display value.

A little tricky solution...

- Cnu
 
I

Ignacio Machin ( .NET/ C# MVP )

Does XtraTreeList allow you any way of converting from a value to the
displayed item? (I'd hope so, for internationalisation purposes if
nothing else.)

Jon

I'm not so sure for the header text. In some cases you have a
InitializeLayout method that you can use for it. Dont' know for this
control though.
It can relay in a resource table for globalization
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum TypeConverter 3
about enum 3
Enum Extentions 7
Question about enum values 10
Merge Info From Two Enums 1
[Flags] Enum -- make it better 1
Display enum in ComboBox with spaces 7
Enum to ListBox 2

Top