Enums

N

Nikolay Petrov

Let's say I have an Enum

Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
Item4 = 4
End

and I have a method which accepts this Enum as parameter:

Sub Test(byval Item as Items)
'Code
End Sub

Is it possbile to provide an integer value, corresponding to the Enumeration
index?
Like: Test(3)
Of course I get an error in IDE,if Option Strict is On, so how to convert
this intger to the corresponding Enum?

TIA
 
E

ECathell

One of the reasons for enums is to make code easier to read. If you are
going to pass the value 3 why not just pass the enum that represents that
value? Thats really the purpose of an enum as I understand them.
 
R

rawCoder

simply use CType whenever you want conversion in between integer and your
enum.

HTH
rawCoder
 
R

Ray Cassick \(Home\)

Actually would it not be better to use DirectCast?

I always get confused between the two (DirectCast and Ctype) and when to use
them.
 
J

José Joye

As a side note, I would mention that casting a 5 to your enum will not
generate any exception. If you do not trust the client of your routine (for
such a problem) it could be worth to check the value is valid.

- José
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top