问:为什么要输入枚举? [英] Q: Why casting an enum?

查看:77
本文介绍了问:为什么要输入枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我创建了一个这样的枚举列表:


enum myEnum:int

{

这个= 2,

那个,

NewVal = 10,

LastItm

}


当使用枚举时,为什么我必须转换值?


int aValue =(int)myEnum.This ;


应该不只是...


int aValue = myEnum.That; ?


问候


Martin Arvidsson

Hi!

I have created an enum list like this:

enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
}

When using the enum, why do i have to cast the value?

int aValue = (int)myEnum.This;

should it not just do with...

int aValue = myEnum.That; ?

Regards

Martin Arvidsson

推荐答案

因为枚举类型的值不是数字。


" Visual Systems AB(Martin Arvidsson)" <毫安************** @ vsab.net> schrieb

im Newsbeitrag新闻:u2 ************* @ TK2MSFTNGP15.phx.gbl ...
Because a value of an enum-type is not a number.

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> schrieb
im Newsbeitrag news:u2*************@TK2MSFTNGP15.phx.gbl...
嗨!

我创建了一个这样的枚举列表:

枚举myEnum:int
{
这= 2,
那,
NewVal = 10,
LastItm
}

使用枚举时,为什么必须转换值?

int aValue =(int)myEnum。这个;

应该不只是...

int aValue = myEnum.That; ?

Martin Arvidsson
Hi!

I have created an enum list like this:

enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
}

When using the enum, why do i have to cast the value?

int aValue = (int)myEnum.This;

should it not just do with...

int aValue = myEnum.That; ?

Regards

Martin Arvidsson



Visual Systems AB(Martin Arvidsson)写道:
Visual Systems AB (Martin Arvidsson) wrote:
嗨!

我创建了一个这样的枚举列表:

枚举myEnum:int
{
这= 2,
那个,
NewVal = 10,
LastItm
}

当使用枚举时,为什么我必须抛出值?

int aValue =(int)myEnum.This;

应该不只是...

int aValue = myEnum.That; ?

马丁·阿尔维德森
Hi!

I have created an enum list like this:

enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
}

When using the enum, why do i have to cast the value?

int aValue = (int)myEnum.This;

should it not just do with...

int aValue = myEnum.That; ?

Regards

Martin Arvidsson




你*应该*使用

myEnum aValue = myEnum.This;


枚举是一个单独的类型,而不仅仅是一种指定整数常量列表的方法。

所以如果你想要将枚举值转换为int,你必须转换...


Hans Kesting



you *should* use
myEnum aValue = myEnum.This;

the enum is a separate type, not just a way to specify a list of integer constants.
So if you want to convert an enum value to an int, you have to cast ...

Hans Kesting


枚举是一个类。请参阅.Net框架中的Enum类。


通常,您不希望将枚举转换为int。如果你这样做,你有明确的

。\


实际上,你应该避免为枚举值赋值。编译器会为你做这个
。 (如果你试图制作一组位标志,请务必使用

[Flags]属性。参见FlagsAttribute类。)


如果是建议您按如下方式使用枚举:


myEnum theEnum = myEnum.That;

if(theEnum == myEnum.LastItm){

//做点什么

}


开关(theEnum){

案例:myEnum.This:

//东西

休息;

案例:myEnum.That:

//其他东西

休息;

// {其余有效枚举值转到此处}

默认值:

抛出新的ArgumentException(&无效theEnum的值是

指定的,theEnum");

}


希望这会有所帮助...


Frisky


" Visual Systems AB(Martin Arvidsson)" <毫安************** @ vsab.net>写在

消息新闻:u2 ************* @ TK2MSFTNGP15.phx.gbl ...
An enum is a class. See the Enum class in the .Net framework.

Normally you do not want to cast an enum to an int. And if you do, you have
to do so explicitly.\

Really, you should avoid assigning values to the enum. The compiler will do
this for you. (If you are trying to make a set of bit flags, be sure to use
the [Flags] attribute. See FlagsAttribute class.)

If is recommended that you would use an enum as follows:

myEnum theEnum = myEnum.That;
if (theEnum == myEnum.LastItm) {
// do something
}

switch (theEnum) {
case: myEnum.This:
// stuff
break;
case: myEnum.That:
// other stuff
break;
// {rest of valid enum values go here}
default:
throw new ArgumentException("Invalid value for theEnum was
specified", "theEnum");
}

Hope this helps...

Frisky

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> wrote in
message news:u2*************@TK2MSFTNGP15.phx.gbl...
嗨!

我创建了一个这样的枚举列表:

枚举myEnum:int
{
这= 2,
那,
NewVal = 10,
LastItm
}

使用枚举时,为什么必须转换值?

int aValue =(int)myEnum。这个;

应该不只是...

int aValue = myEnum.That; ?

Martin Arvidsson
Hi!

I have created an enum list like this:

enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
}

When using the enum, why do i have to cast the value?

int aValue = (int)myEnum.This;

should it not just do with...

int aValue = myEnum.That; ?

Regards

Martin Arvidsson



这篇关于问:为什么要输入枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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