Results 1 to 10 of 10

Thread: [RESOLVED] Convert enum string Value to integer Value

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Resolved [RESOLVED] Convert enum string Value to integer Value

    VS 2005

    How do I convert a variable which is a string into the integer value of its emun value. ie

    I have the following variables:
    objValue has a string value of sdoSI
    sdoType is an enum in which sdoSI has a value of 1

    I want to change the value of objValue from string "sdoSI" to integer "1"
    Last edited by chilling; Apr 11th, 2006 at 04:40 PM.
    chilling

  2. #2
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: Convert enum string Value to integer Value

    VB Code:
    1. Dim obj As String = "sdoSI"
    2.         If obj = "sdoSI" Then
    3.             obj = CInt(1)
    4.         Else
    5.             obj = CInt(0)
    6.         End If

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092

    Re: Convert enum string Value to integer Value

    Code:
        Private Enum SomeEnum
            Value1
            Value2
        End Enum
    
        Private Function ConvertStringToEnumValue(ByVal value As String) As SomeEnum
            Return CType(System.ComponentModel.TypeDescriptor.GetConverter(GetType(SomeEnum)).ConvertFromString(value), SomeEnum)
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(ConvertStringToEnumValue("Value2"))
        End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Convert enum string Value to integer Value

    Quote Originally Posted by crptcblade
    Code:
        Private Enum SomeEnum
            Value1
            Value2
        End Enum
    
        Private Function ConvertStringToEnumValue(ByVal value As String) As SomeEnum
            Return CType(System.ComponentModel.TypeDescriptor.GetConverter(GetType(SomeEnum)).ConvertFromString(value), SomeEnum)
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(ConvertStringToEnumValue("Value2"))
        End Sub
    And I thought you only knew how to chit chat

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: Convert enum string Value to integer Value

    I realise I can do that ... but I would need to do it as a SELECT CASE statement because there are about 50 different values of the enum of which sdoSI is the first.

    I need to look up the integer value of the enum and then save that into objValue.
    chilling

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Convert enum string Value to integer Value

    Is this what you want?
    VB Code:
    1. Public Enum MyEnum
    2.     Value1 = 0
    3.     Value2 = 1
    4.     Value3 = 2
    5. End Enum
    6. Public Class Form1
    7.  
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         Dim a As MyEnum = MyEnum.Value2
    10.  
    11.         MessageBox.Show(CInt(a).ToString)
    12.     End Sub
    13. End Class
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: Convert enum string Value to integer Value

    Thanks crptcblade - that has done exactly what I needed

    mpdeglau - thanks for the suggestion, I'd tried something very similar and it was not giving me the result I needed
    chilling

  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [RESOLVED] Convert enum string Value to integer Value

    Just an FYI, my code produces the same result as crptcblade, with less code. You don't need to give the enum values yourself, that's just a good habit to do, especially if you plan on getting their int values.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    163

    Re: [RESOLVED] Convert enum string Value to integer Value

    Quote Originally Posted by mpdeglau
    Just an FYI, my code produces the same result as crptcblade, with less code. You don't need to give the enum values yourself, that's just a good habit to do, especially if you plan on getting their int values.
    Must have just been my bad programming or lack of knowledge
    chilling

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092

    Re: [RESOLVED] Convert enum string Value to integer Value

    Quote Originally Posted by mpdeglau
    Just an FYI, my code produces the same result as crptcblade, with less code. You don't need to give the enum values yourself, that's just a good habit to do, especially if you plan on getting their int values.
    Code:
    MessageBox.Show("1")
    There's the same result with even less code.

    Just because the result is the same, that doesn't necessarily mean its the same solution. The original problem, as I understood it, was that he had the string "Value2", and wanted to get the value of the enumeration's Value2 member. Sure if you've got MyEnum.Value2, you can easily treat it as an integer, but your solution doesn't convert a string with the same name as a enum member and convert it to the actual value of that enum member.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

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