Enum Oddity [英] Enum Oddity

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

问题描述

大家好


我肯定有一个合理的解释,但是......


< code>

Public Enum Test

Abra = 5

Cadabra = 76

End Enum

Dim t作为测试


t = Test.Cadabra

< / code>


在这结束时,我希望包含76,但它不是。它包含

Cadabra。


在命令窗口中,我得到以下


?t
Cadabra


另外,


?CInt(t)




为什么我必须显式转换为Int?毕竟,我认为这是

只是一个很好的方法让intellisense提示我的值为什么时候

我输入


t =


有谁可以解释一下?


TIA


Charles

解决方案

*" Charles Law" < BL *** @ nowhere.com> scripsit:

我相信这有一个合理的解释,但是......

< code>
Public Enum Test
Abra = 5
Cadabra = 76
结束枚举

Dim t as Test

t = Test.Cadabra
< / code>

最后我希望包含76,但它不是。它包含
Cadabra。


''Cadabra''是一个值为76的常量。

在命令窗口中,我得到以下

? t> Cadabra

或者,

?CInt(t)
76

为什么我必须明确转换为诠释?毕竟,我认为这只是一个很好的方法来让intellisense提示我输入值的时候
我输入




我认为控制台只会调用对象的对象。 ''ToString''方法

将返回枚举常量的名称(AFAIR)。


-

Herfried K. Wagner [MVP]

< http://www.mvps.org/dotnet>


" Charles Law" < BL *** @ nowhere.com> schrieb

嗨大家好

我相信这有一个合理的解释,但是......

< code> Abra = 5
Cadabra = 76
结束枚举

Dim t as Test

t = Test.Cadabra
< / code>

最后,我希望包含76,但它不是。它包含Cadabra。

在命令窗口,我得到以下

?t
Cadabra

或者,

?CInt(t)
76

为什么我必须显式转换为Int?毕竟,我认为这只是一个很好的方式来让intellisense在我输入时提示我输入值

t =
任何人都可以解释一下吗?



如果打印枚举变量的值,会显示成员名称,因为

表示你​​是什么(或者我通常感兴趣。使用枚举

的主要原因是能够使用名称而不必记住

数值。你仍然可以使用Cint来获得价值。

-

Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


你好Herfried


啊,但是为什么下面这样做呢?


< code>

Dim str As String


str = String.Format(" t = {0}",t)

< / code>


str现在包含t = Cadabra。这是非常痛苦的,不要说容易出错如果

我必须记得写下来


< code>

Dim str as String


str = String.Format(" t = {0}",CInt(t))

< / code>


或者你能期待什么?


Charles

" Herfried K. Wagner [MVP]" ; <喜*************** @ gmx.at>在消息中写道

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

*" ; Charles Law < BL *** @ nowhere.com> scripsit:

我相信这有一个合理的解释,但是......

< code>
Public Enum Test
Abra = 5
Cadabra = 76
结束枚举

Dim t as Test

t = Test.Cadabra
< / code>

最后我希望包含76,但它不是。它包含了
Cadabra。



''Cadabra''是一个值为76的常量。

在命令窗口中,我得到以下内容

?Cadabra

或者,

?CInt(t)
76

为什么我必须显式转换为Int?毕竟,我认为这个
只是一个很好的方法来让intellisense在输入时提示我输入
的值

我认为控制台会只需调用对象的对象即可。 ''ToString''方法
将返回枚举常量的名称(AFAIR)。

-
Herfried K. Wagner [MVP]
< http://www.mvps.org/dotnet>



Hi Guys

I''m sure there is a plausible explanation for this, but ...

<code>
Public Enum Test
Abra = 5
Cadabra = 76
End Enum

Dim t as Test

t = Test.Cadabra
</code>

At the end of this I expect t to contain 76, but it doesn''t. It contains
Cadabra.

In the command window, I get the following

?t
Cadabra

Alternatively,

?CInt(t)
76

Why do I have to explicitly convert to Int? After all, I thought this was
just a good way to get the intellisense to prompt me with values for t when
I type

t =

Can anyone explain?

TIA

Charles

解决方案

* "Charles Law" <bl***@nowhere.com> scripsit:

I''m sure there is a plausible explanation for this, but ...

<code>
Public Enum Test
Abra = 5
Cadabra = 76
End Enum

Dim t as Test

t = Test.Cadabra
</code>

At the end of this I expect t to contain 76, but it doesn''t. It contains
Cadabra.
''Cadabra'' is a constant with value 76.
In the command window, I get the following

?t
Cadabra

Alternatively,

?CInt(t)
76

Why do I have to explicitly convert to Int? After all, I thought this was
just a good way to get the intellisense to prompt me with values for t when
I type



I think the console will simply call the "object''s" ''ToString'' method
which will return the name of the constant for an enumeration (AFAIR).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


"Charles Law" <bl***@nowhere.com> schrieb

Hi Guys

I''m sure there is a plausible explanation for this, but ...

<code>
Public Enum Test
Abra = 5
Cadabra = 76
End Enum

Dim t as Test

t = Test.Cadabra
</code>

At the end of this I expect t to contain 76, but it doesn''t. It
contains Cadabra.

In the command window, I get the following

?t
Cadabra

Alternatively,

?CInt(t)
76

Why do I have to explicitly convert to Int? After all, I thought this
was just a good way to get the intellisense to prompt me with values
for t when I type

t =

Can anyone explain?


If you print the value of an enum variable, the member name is shown because
that''s what you are (or I am) usually interested in. The main reason for
using enums is being able to use a name instead of having to remember a
numeric value. Still you can use Cint to get the value behind.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Hi Herfried

Ah, but why does the following do the same?

<code>
Dim str As String

str = String.Format("t={0}", t)
</code>

str now contains "t=Cadabra". It is very painful, not to say error prone if
I have to remember to write

<code>
Dim str As String

str = String.Format("t={0}", CInt(t))
</code>

Or is that what you would expect?

Charles
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

* "Charles Law" <bl***@nowhere.com> scripsit:

I''m sure there is a plausible explanation for this, but ...

<code>
Public Enum Test
Abra = 5
Cadabra = 76
End Enum

Dim t as Test

t = Test.Cadabra
</code>

At the end of this I expect t to contain 76, but it doesn''t. It contains
Cadabra.



''Cadabra'' is a constant with value 76.

In the command window, I get the following

?t
Cadabra

Alternatively,

?CInt(t)
76

Why do I have to explicitly convert to Int? After all, I thought this was just a good way to get the intellisense to prompt me with values for t when I type



I think the console will simply call the "object''s" ''ToString'' method
which will return the name of the constant for an enumeration (AFAIR).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>



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

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