473,738 Members | 11,192 Online
Bytes | Software Development & Data Engineering Community
+ 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 1239
* "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******** ********@TK2MSF TNGP11.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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 MyStructInstanc e As MyStruct

MyStructInstanc e.Param2 = Abra Or Cadabra

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

MyStructInstanc e.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*******@free net.de> wrote in message
news:eS******** *****@TK2MSFTNG P11.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 MyStructInstanc e As MyStruct

MyStructInstanc e.Param2 = Abra Or Cadabra

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

MyStructInstanc e.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

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

Similar topics

12
2403
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
4600
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
3614
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
0
709
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
12391
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
2460
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 installed the boost library. This is using gcc under FC3.
2
2120
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 installed the boost library. This is using gcc under FC3.
5
1789
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 or a Safari oddity (which if it is, any top-tips to fix it would be most welcome!). Objective: I want to have the meat of the viewport divided into 3 columns (a fixed margin (say 140px) and 2 scalable panes on the right each of which
34
11196
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.