Groups | Blog | Home
all groups > vb.net > january 2004 >

vb.net : Enum Oddity



Jay B. Harlow [MVP - Outlook]
1/23/2004 6:14:44 PM
Charles,
In addition to the others comments:

[quoted text, click to view]

Don't think of an Enum in terms of its implementation!

Don't think of Enums as integers! Think of Enums as distinct named constants
that are of that specific Enum type. As that is IMHO what the intent of
Enums are. If you want integer constants use the Const keyword.

Enums just happen to be implemented as Integers. (which for performance
reasons make sense, otherwise it does not make sense, I can see lots of
value in String enums or Char enums!)

In your example: Cadabra is a specific value for Test, as is Abra.

Hope this helps
Jay

[quoted text, click to view]

Charles Law
1/23/2004 11:35:33 PM
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
1/24/2004 12:00:58 AM
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


[quoted text, click to view]

Armin Zingler
1/24/2004 12:46:40 AM
"Charles Law" <blank@nowhere.com> schrieb
[quoted text, click to view]


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
hirf-spam-me-here NO[at]SPAM gmx.at
1/24/2004 12:50:59 AM
* "Charles Law" <blank@nowhere.com> scripsit:
[quoted text, click to view]

'Cadabra' is a constant with value 76.

[quoted text, click to view]

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]
Charles Law
1/24/2004 1:20:06 AM
Thanks Jay, and everyone else for the responses.

I see what it is doing now, and I also use enums in the way you describe. I
will just have to be careful when I want the actual value of the enum.

Cheers

Charles


[quoted text, click to view]

hirf-spam-me-here NO[at]SPAM gmx.at
1/24/2004 1:59:50 AM
* "Charles Law" <blank@nowhere.com> scripsit:
[quoted text, click to view]

I would expect that.

--
Herfried K. Wagner [MVP]
Armin Zingler
1/24/2004 8:40:14 AM
"Charles Law" <blank@nowhere.com> schrieb
[quoted text, click to view]

Also what I would expect.

Why do you use an enum if you are interested in the numeric values? (see
also my other post)


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Jay B. Harlow [MVP - Outlook]
1/24/2004 9:36:08 AM
Charles,

[quoted text, click to view]

What I normally do in that case is define const members of a class

[quoted text, click to view]

Private Sub New()
End Sub

[quoted text, click to view]

Of course I need to type the name of the class, however I then get the names
of the constants...

Note the Notinheritable prevents others from inheriting the Special class,
while Private Sub New prevents others form instantiating the Special class,
as Special is intended only to offer constants.

Hope this helps
Jay

[quoted text, click to view]

Charles Law
1/24/2004 9:49:44 AM
Hi Armin

So I get the advantage of intellisense. When I type

MyVal =

I want the convenience of a list popping up to choose from.

It is rare that I need the underlying value, as people have indicated, it is
not common to use them in this way. However, I have the case where a
parameter in a structure can take values from a set list, e.g.

Public Enum Special
Abra = &H1
Cadabra = &H2
End Enum

Public Structure MyStruct
Dim Param1 as Integer
Dim Param2 as Special
End Structure

Then, I may want to do this

Dim MyStructInstance As MyStruct

MyStructInstance.Param2 = Abra Or Cadabra

Should Param2 now contain 3? It probably will in this case. But what about

MyStructInstance.Param2 = Abra

Now, perhaps Param2 contains Abra. I haven't tried this, but this is the
type of thing I wish to do.

Am I going about this the wrong way? The values will not always be binary as
above, but sometimes. How would you do it?

Charles


[quoted text, click to view]

Armin Zingler
1/24/2004 12:40:32 PM
"Charles Law" <blank@nowhere.com> schrieb
[quoted text, click to view]

I'd do it the same way. Just wanted to explain why the Enum member's name
instead of it's value is displayed by default.


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Charles Law
1/24/2004 5:26:13 PM
A good bit of lateral thinking.

Thanks again.

Charles


[quoted text, click to view]

AddThis Social Bookmark Button