Results 1 to 11 of 11

Thread: Convert and Enum to integer?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Convert and Enum to integer?

    say I have this:

    Enum Alpha
    A
    B
    C
    End Enum

    Is there a way to do this same thing without using Cint:

    Dim val As Ineger = CInt(Alpha.A)

    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    say I have this:

    Enum Alpha
    A
    B
    C
    End Enum

    Is there a way to do this same thing without using Cint:

    Dim val As Ineger = CInt(Alpha.A)

    It returns integer as default datatype . So you don't have to convert it .

    VB Code:
    1. Dim val As Ineger = Alpha.A

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Heres an example of an enum.

    VB Code:
    1. Friend Enum enumDatabaseType As Byte
    2.         DBAccess97 = 1
    3.         DBAccess2000 = 2
    4.         DBSQLServer = 3
    5.     End Enum

    When you use it, its not a number, or an integer, its an enum type. If you do "MyVal.ToString" it will result in "DBAccess2000" for example, but if you want the number, it will convert it for you (as the format you specify like above).
    Last edited by Grimfort; Jan 13th, 2004 at 08:40 AM.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Grimfort
    When you use it, its not a number, or an integer, its an enum type. If you do "MyVal.ToString" it will result in "DBAccess2000" for example, but if you want the number, it will convert it for you (as above).
    NO
    Enum are basically integral values . If you didn't provide the values for each one , they start counting from zero . Remember Enums can't be string values but can be converted to string as you just did .

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    May I suggest you read this:

    http://support.microsoft.com/default...n-us;311327#1e

    Main part being:
    Enumerators are value types that have their own set of methods.

    You can also use them alongside reflection to use things like ModuleBuilder.DefineEnum, and EnumBuilder.DefineLiteral (Ive not used them personally tho).


    Edit: Notice the edit in the post b4, I wanted to make it more clear.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Grimfort
    May I suggest you read this:

    http://support.microsoft.com/default...n-us;311327#1e

    Main part being:
    Enumerators are value types that have their own set of methods.

    You can also use them alongside reflection to use things like ModuleBuilder.DefineEnum, and EnumBuilder.DefineLiteral (Ive not used them personally tho).

    Edit: Notice the edit in the post b4, I wanted to make it more clear.
    I can't see any relation to the problem presented here . The point is : No explicity conversion is needed since the default type of declared enum is integer .

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    I agree. What were you saying no to then ? I just said that an enum is of type enum, that automatically converts to the declared format.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Grimfort

    When you use it, its not a number, or an integer, its an enum type. If you do "MyVal.ToString" it will result in "DBAccess2000" for example, but if you want the number, it will convert it for you (as the format you specify like above).
    I said no because conversion isn't necessary her (as you did).

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Thats why I edited my post, it wasnt clear enough. I thought you were saying no, that an enum was not a type, but "basically integral values". I didnt mean to sound shirty.

  10. #10

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    aah you too stop arguing

    hehe I didnt know I can get by it without CInt hehe thanks
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    It was a technical discussion . Nothing like a little heat to warm the forums.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width