473,762 Members | 6,675 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
11 1241
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******** ******@TK2MSFTN GP12.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 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 #11
A good bit of lateral thinking.

Thanks again.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP12.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 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 #12

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

Similar topics

12
2404
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
4601
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
3616
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
711
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
12392
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
1790
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
11201
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
9377
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
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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
9925
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
9811
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
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 we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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.