enum.ToString() [英] enum.ToString()

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

问题描述

ToString()函数,当应用于枚举

类型的变量时,会产生一个字符串,它是枚举值的名称

在源代码中定义。这很酷,但如何覆盖

函数让某些值返回不同的字符串?


例如,假设我有一个值为枚举的枚举对于DVD媒体的类型

喜欢


公共枚举DVD_Media

{

DVD_ROM = 1 ,

DVD_R,

DVD_RW,

DVD_R_Plus,

DVD_RW_Plus,

DVD_RAM

}


但我希望将值打印为DVD-ROM,DVD-R,DVD-RW, DVD + R,

DVD + RW和DVD-RAM。 - C#语法似乎不允许我使用

机制来覆盖此特定枚举的ToString()方法。


-Ken

The ToString() function, when applied to a variable that is an enumeration
type, results in a string that is the name of the enumerated value that was
defined in the source code. This is cool, but how does one override the
function to have some values return a different string?

For example, suppose I have an enum with value for the types of DVD media
like

public enum DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus,
DVD_RAM
}

but I want the values to print as "DVD-ROM", "DVD-R", "DVD-RW", "DVD+R",
"DVD+RW", and "DVD-RAM" -- the C# syntax does not seem to permit me a
mechanism to override the ToString() method on this specific enumeration.

-Ken

推荐答案

Ken


你可以考虑创建一个函数,它使用
$返回资源字符串b $ b作为参数枚举值并使用此函数而不是ToString()。

enum.ToString()可用于标识资源字符串。


HTH

Alex


" Ken Allen" <柯****** @ sympatico.ca>在消息中写道

news:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
Hi, Ken

you might consider creating a function, which returns resource string using
as argument enum value and use this function instead of ToString().
enum.ToString() could be used to identify resource strings.

HTH
Alex

"Ken Allen" <ke******@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
ToString ()函数,当应用于枚举
类型的变量时,会产生一个字符串,该字符串是源代码中定义
的枚举值的名称。这很酷,但是如何覆盖
函数以使某些值返回不同的字符串?

例如,假设我有一个具有DVD媒体类型值的枚举


公共枚举DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus ,
DVD_RAM


但是我希望将值打印为DVD-ROM,DVD-R,DVD-RW, DVD + R,
DVD + RW和DVD-RAM。 - C#语法似乎不允许我使用
机制来覆盖此特定枚举的ToString()方法。

-Ken
The ToString() function, when applied to a variable that is an enumeration
type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but how does one override the
function to have some values return a different string?

For example, suppose I have an enum with value for the types of DVD media
like

public enum DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus,
DVD_RAM
}

but I want the values to print as "DVD-ROM", "DVD-R", "DVD-RW", "DVD+R",
"DVD+RW", and "DVD-RAM" -- the C# syntax does not seem to permit me a
mechanism to override the ToString() method on this specific enumeration.

-Ken


Ken,


尝试Enum.GetNames(typeof(A_CTS_Type))

====== ==============

RBisch - C#发烧友
ryanbischoff@PLZyahooNO_SPAM.com

" Ken Allen" <柯****** @ sympatico.ca>在消息中写道

news:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
Ken,

Try Enum.GetNames(typeof(A_CTS_Type))
====================
RBisch - C# enthusiast
ryanbischoff@PLZyahooNO_SPAM.com
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
ToString ()函数,当应用于枚举
类型的变量时,会产生一个字符串,该字符串是源代码中定义
的枚举值的名称。这很酷,但是如何覆盖
函数以使某些值返回不同的字符串?

例如,假设我有一个具有DVD媒体类型值的枚举


公共枚举DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus ,
DVD_RAM


但是我希望将值打印为DVD-ROM,DVD-R,DVD-RW, DVD + R,
DVD + RW和DVD-RAM。 - C#语法似乎不允许我使用
机制来覆盖此特定枚举的ToString()方法。

-Ken
The ToString() function, when applied to a variable that is an enumeration
type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but how does one override the
function to have some values return a different string?

For example, suppose I have an enum with value for the types of DVD media
like

public enum DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus,
DVD_RAM
}

but I want the values to print as "DVD-ROM", "DVD-R", "DVD-RW", "DVD+R",
"DVD+RW", and "DVD-RAM" -- the C# syntax does not seem to permit me a
mechanism to override the ToString() method on this specific enumeration.

-Ken


Alex和Ken,


我认为使用函数是一个坏主意。并不是因为它是错误的做法,而是有更好的解决方案适合.NET提供的模型

。也就是说,您应该使用属性来枚举枚举中的

元素。对于这样的事情,我更喜欢System.ComponentModel命名空间中的

Description属性。有了它,你可以做到这一点:


公共枚举MyEnum

{

[描述(我的第一个价值。)]

价值,

[描述(我的第二个价值。)]

价值2

}


执行此操作后,您可以使用枚举上的反射来获取

字段(值为实际上只是静态字段)然后得到

属性,然后是描述。


这样做的好处是有关

枚举与枚举保持一致。另外,你可以有一个

例程,它会给你提供信息,就是这样,当你想要改变它时,你不会有
来改变它改变描述。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C# MVP]

- mv*@spam.guard.caspershouse.com


" AlexS" < SA *********** @ SPAMsympaticoPLEASE.ca>写在留言中

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Alex and Ken,

I think that using a function is a bad idea. Not because it is the
wrong thing to do, but there are better solutions that fit into the model of
what .NET offers. Namely, you should use attributes to attribute the
elements in the enumeration. For something like this, I prefer the
Description attribute in the System.ComponentModel namespace. With it, you
can do this:

public enum MyEnum
{
[Description("My first value.")]
Value,
[Description("My second value.")]
Value2
}

Once you do this, you can use reflection on the enumeration to get the
fields (the values are really just static fields) and then get the
attributes, and subsequently the description.

The advantage to doing this is that the information about the
enumeration stays with the enumeration. Additionally, you can have one
routine which will give you the information, and that''s it, you won''t have
to change it when you want to change the description.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
你可能会考虑创建一个函数,它使用参数枚举值返回资源字符串
并使用此函数而不是ToString()。
enum.ToString()可能是用于识别资源字符串。

HTH
Alex

Ken Allen <柯****** @ sympatico.ca>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...
Hi, Ken

you might consider creating a function, which returns resource string using as argument enum value and use this function instead of ToString().
enum.ToString() could be used to identify resource strings.

HTH
Alex

"Ken Allen" <ke******@sympatico.ca> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
ToString()函数,当应用于
枚举类型的变量时,会产生一个字符串,该字符串是
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that


在源代码中定义的


was

的枚举值的名称。这很酷,但是如何覆盖
函数以使某些值返回不同的字符串?

例如,假设我有一个包含DVD $ b $类型值的枚举b媒体如

公共枚举DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus ,
DVD_RAM


但是我希望将值打印为DVD-ROM,DVD-R,DVD-RW, DVD + R,
DVD + RW和DVD-RAM。 - C#语法似乎不允许我使用
机制来覆盖此特定
枚举的ToString()方法。
-Ken
defined in the source code. This is cool, but how does one override the
function to have some values return a different string?

For example, suppose I have an enum with value for the types of DVD media like

public enum DVD_Media
{
DVD_ROM = 1,
DVD_R,
DVD_RW,
DVD_R_Plus,
DVD_RW_Plus,
DVD_RAM
}

but I want the values to print as "DVD-ROM", "DVD-R", "DVD-RW", "DVD+R",
"DVD+RW", and "DVD-RAM" -- the C# syntax does not seem to permit me a
mechanism to override the ToString() method on this specific enumeration.
-Ken



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

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