473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Enums

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
Nov 21 '05 #1
5 1170
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.
--
--Eric Cathell, MCSA
"Nikolay Petrov" <jo**************@mail.bg> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2
simply use CType whenever you want conversion in between integer and your
enum.

HTH
rawCoder

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
--
--Eric Cathell, MCSA
"Nikolay Petrov" <jo**************@mail.bg> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #3
Actually would it not be better to use DirectCast?

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

"rawCoder" <ra******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
simply use CType whenever you want conversion in between integer and your
enum.

HTH
rawCoder

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
--
--Eric Cathell, MCSA
"Nikolay Petrov" <jo**************@mail.bg> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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
>
>



Nov 21 '05 #4
Yes DirectCast is generally considered faster as it only does CASTING and
not conversion as compared to CType.

Here are couple of links that might explain more
http://msdn.microsoft.com/library/de...tinternals.asp

http://groups-beta.google.com/group/...b28bc12e70c56d

http://groups-beta.google.com/group/...ceb63f71df6bb1

HTH
rawCoder

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:uA**************@TK2MSFTNGP15.phx.gbl...
Actually would it not be better to use DirectCast?

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

"rawCoder" <ra******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
simply use CType whenever you want conversion in between integer and your enum.

HTH
rawCoder

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
--
--Eric Cathell, MCSA
"Nikolay Petrov" <jo**************@mail.bg> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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
>
>



Nov 21 '05 #5
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é
"rawCoder" <ra******@hotmail.com> a écrit dans le message de news:
%2***************@TK2MSFTNGP10.phx.gbl...
simply use CType whenever you want conversion in between integer and your
enum.

HTH
rawCoder

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
--
--Eric Cathell, MCSA
"Nikolay Petrov" <jo**************@mail.bg> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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
>
>



Nov 21 '05 #6

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

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
13
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.