Enum的空间 [英] Spaces in Enum

查看:74
本文介绍了Enum的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

是否可以在枚举值中包含空格?

我有这样的枚举:


public enum Columns

{Italy = 1,OtherCountryUE = 2,OtherCountryOCSE = 3,OtherCountr =

4}


和我想要


公共枚举列

{意大利= 1,其他国家UE = 2,其他国家OCSE = 3,其他

Countr = 4}


但此表格无效。


如何解决?


非常感谢。

-

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.
--
Luigi

推荐答案

9月24日上午11:48 *,Luigi< ciupazNoSpamGra ... @ inwind.itwrote:
On Sep 24, 11:48*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:

是否可以有空格在枚举值?
is it possible to have spaces in enum values?



编号名称必须是有效的C#标识符。如果你想要一个人类可读的表格,你可能希望为每个值提供

属性。


Jon

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


Luigi把它放在他的屏幕上:
Luigi laid this down on his screen :

大家好,

是否有可能拥有枚举值中的空格?

我有这样的枚举:


公共枚举列

{Italy = 1,OtherCountryUE = 2,OtherCountryOCSE = 3,OtherCountr =

4}


我想要


public enum Columns

{意大利= 1,其他国家UE = 2,其他国家OCSE = 3,其他

Countr = 4}


但此表格无效。


如何解决?


非常感谢。
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.



你不能。枚举值必须是标识符,因此您必须使用

标识符的命名规则。这意味着:没有空格。


你为什么要这样?


Hans Kesting

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

9月24日,6:55 * am,Jon Skeet [C#MVP] < sk ... @ pobox.comwrote:
On Sep 24, 6:55*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:

9月24日,11:48 * am,Luigi< ciupazNoSpamGra ... @ inwind.itwrote:
On Sep 24, 11:48*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:

是否可以在枚举值中包含空格?
is it possible to have spaces in enum values?



编号名称必须是有效的C#标识符。如果你想要一个人类可读的表格,你可能希望为每个值提供

属性。


Jon


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



我认为这是出于显示目的(填充下拉列表或类似内容)。


我这样快速和肮脏:


//这是我的枚举

公共枚举状态

{

Not_selected = 0,

待定= 1,

完成= 2,

Re__Notification_Required //注意:__后来替换为 -

显示用途; _被替换为空格

}

//这是一个返回字典的助手~~~:make itgeneric

public static Dictionary< ; int,stringGetCaseFlowStatusSelection()

{

Dictionary< int,stringret = new Dictionary< int,string>();

foreach(状态Enum.GetValues中的状态(typeof(Status)))

ret.Add((int)status,status.ToString()。Replace(" __",

" - ")。替换(_,");;

返回ret;

}

/ /消耗解决方案:-)

this.cmbCurrentStatus.DataSource = new

BindingSource(MyClass.GetCaseFlowStatusSelection(),null);

this .cmbCurrentStatus.DisplayMember =" Value";

this.cmbCurrentStatus.ValueMember =" Key";

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, stringGetCaseFlowStatusSelection()
{
Dictionary<int, stringret = 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.


这篇关于Enum的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆