473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Enum Oddity

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
Nov 20 '05 #1
11 1207
* "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>
Nov 20 '05 #2
"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

Nov 20 '05 #3
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>

Nov 20 '05 #4
Charles,
In addition to the others comments:
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
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl... 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

Nov 20 '05 #5
* "Charles Law" <bl***@nowhere.com> scripsit:
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?


I would expect that.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
Charles,
In addition to the others comments:
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


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

"Charles Law" <bl***@nowhere.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #7
"Charles Law" <bl***@nowhere.com> schrieb
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?


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

Nov 20 '05 #8
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
"Armin Zingler" <az*******@freenet.de> wrote in message
news:eS*************@TK2MSFTNGP11.phx.gbl...
"Charles Law" <bl***@nowhere.com> schrieb
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?


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

Nov 20 '05 #9
"Charles Law" <bl***@nowhere.com> schrieb
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?


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

Nov 20 '05 #10
Charles,
So I get the advantage of intellisense. When I type
What I normally do in that case is define const members of a class
Public NotInheritable Class Special
Const Abra As Integer = &H1
Const Cadabra As Integer = &H2
Private Sub New()
End Sub
End Class MyVal = Special.
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ep**************@TK2MSFTNGP12.phx.gbl... 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
"Armin Zingler" <az*******@freenet.de> wrote in message
news:eS*************@TK2MSFTNGP11.phx.gbl...
"Charles Law" <bl***@nowhere.com> schrieb
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?


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


Nov 20 '05 #11
A good bit of lateral thinking.

Thanks again.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Charles,
So I get the advantage of intellisense. When I type
What I normally do in that case is define const members of a class
Public NotInheritable Class Special
Const Abra As Integer = &H1
Const Cadabra As Integer = &H2


Private Sub New()
End Sub
End Class

MyVal = Special.


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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ep**************@TK2MSFTNGP12.phx.gbl...
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
"Armin Zingler" <az*******@freenet.de> wrote in message
news:eS*************@TK2MSFTNGP11.phx.gbl...
"Charles Law" <bl***@nowhere.com> schrieb
> 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?

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



Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
0
by: Peter Oliphant | last post by:
If I create an enum ala: enum myEnum { Element1=0, Element2, Element3 } ; I get an error if I reference the enum by qualifying its scope, ala:
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.