Enum Oddity

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

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 said:
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.
 
* "Charles Law said:
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).
 
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
 
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 said:
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.
 
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
 
Charles Law said:
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)
 
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
 
Charles Law said:
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.
 
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
 
Back
Top