locked
return enum value RRS feed

  • Question

  • I am wanting a function that will return the value of a an enum, the case below.. "Data1" is passed to the function and the function would return 1

    Public Enum MyData

            Data1 = 1

            DAta2 = 2

    end enum

    Friday, April 25, 2014 4:05 PM

Answers

  • Ah, then you want Enum.TryParse(). No function required:

        Private Sub TestEnums()
            Dim StringEnum As String = "Data1"
            Dim SomeEnum As MyData
            [Enum].TryParse(StringEnum, SomeEnum)
            MessageBox.Show(SomeEnum & ", " & SomeEnum.ToString())
        End Sub
    
        Public Enum MyData As Integer
            Data1 = 1
            Data2 = 2
        End Enum

    Enum.TryParse() returns a boolean indicating success. It accepts a value to attempt to parse, as well as where the value should go if the parsing is successful.

    • Marked as answer by LandLord323 Friday, April 25, 2014 4:36 PM
    Friday, April 25, 2014 4:26 PM

All replies

  • Hello,

    This

    Public Function test(sender As MyData) As Integer
        Return sender
    End Function

    When called

    Console.WriteLine(test(MyData.Data1))
    Returns 1


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, April 25, 2014 4:09 PM
  • No special functions required. If you declare your enum as an Integer, you the enum value IS the integer. See example:

        Private Sub TestEnums()
            Dim SomeEnum As MyData = MyData.Data1
            MessageBox.Show(SomeEnum)
            MessageBox.Show(SomeEnum.ToString())
        End Sub
    
        Public Enum MyData As Integer
            Data1 = 1
            Data2 = 2
        End Enum

    Friday, April 25, 2014 4:17 PM
  • Ok.. this might help a bit more... I am using the Data1, Data2 in a combobox....  I want the user to select Data1 or Data2 and return the numberic value so I can do an update in a database.  so I thinking I need to pass to the function, the string value of the combobox and somehow get the interger value back.
    Friday, April 25, 2014 4:19 PM
  • Ok.. this might help a bit more... I am using the Data1, Data2 in a combobox....  I want the user to select Data1 or Data2 and return the numberic value so I can do an update in a database.  so I thinking I need to pass to the function, the string value of the combobox and somehow get the interger value back.

    You're already there - that's what they're trying to say.

    Public Enum MyData
        Data1 = 1
        DAta2 = 2
    End Enum

    Then if I run this:

    Dim test As MyData = MyData.DAta2
    Dim test2 As Integer = test
    Stop

    test2 equals the integer value of two (2). Make sense?

    Please call me Frank :)

    Friday, April 25, 2014 4:25 PM
  • Ah, then you want Enum.TryParse(). No function required:

        Private Sub TestEnums()
            Dim StringEnum As String = "Data1"
            Dim SomeEnum As MyData
            [Enum].TryParse(StringEnum, SomeEnum)
            MessageBox.Show(SomeEnum & ", " & SomeEnum.ToString())
        End Sub
    
        Public Enum MyData As Integer
            Data1 = 1
            Data2 = 2
        End Enum

    Enum.TryParse() returns a boolean indicating success. It accepts a value to attempt to parse, as well as where the value should go if the parsing is successful.

    • Marked as answer by LandLord323 Friday, April 25, 2014 4:36 PM
    Friday, April 25, 2014 4:26 PM
  • If you loaded the ComboBox with the members via GetNames then we have

    So if Data1 is selected we need to convert the Text property of the ComboBox to an Enum which can be done by placing the following code into a code module, not a class or form file.

    ''' <summary>
    ''' Convert a string to an enum member
    ''' </summary>
    ''' <typeparam name="T">Enum</typeparam>
    ''' <param name="sender">String value that represents a valid enum member for T</param>
    ''' <returns>Enum member or throws an exception if sender is not valid for T</returns>
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToEnum(Of T)(ByVal sender As String) As T
    
        Dim TheType As Type = GetType(T)
    
        If [Enum].IsDefined(GetType(T), sender) Then
            Return CType(System.Enum.Parse(TheType, sender, True), T)
        Else
    
            Dim BaseType As String = TheType.ToString
            Dim Pos As Integer = BaseType.IndexOf("+")
            Dim ErrorMessage As String = ""
    
            If Pos > -1 Then
                Dim EnumName As String = BaseType.Substring(Pos + 1)
                ErrorMessage = String.Format("'{0}' not a member of '{1}'", sender, EnumName)
            Else
                ErrorMessage = String.Format("{0} not a member of {1}", sender, TheType)
            End If
    
            Throw New Exception(ErrorMessage)
    
        End If
    End Function
    
    

    Place a button on the form and use this

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Demo(ComboBox1.Text.ToEnum(Of MyData)())
    End Sub
    Public Sub Demo(ByVal SomeValue As Integer)
        Console.WriteLine(SomeValue)
    End Sub

    Now what I like to do is use an extension method to populate the ComboBox

    ComboBox1.DataSource = GetType(MyData).Names.ToList

    Extension method for the above

    ''' <summary>
    ''' Return list of names for a specific enum
    ''' </summary>
    ''' <param name="sender">String</param>
    ''' <returns>String array of enum names</returns>
    <System.Runtime.CompilerServices.Extension> _
    Public Function Names(ByVal sender As Type) As String()
        Return System.Enum.GetNames(sender)
    End Function


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, April 25, 2014 4:36 PM
  • Thanks Chip....

    I was not thinking.. I was doing

    console.writeline([enum].tryparse(stringenum, someenum)

    Thanks again for all the help

    Friday, April 25, 2014 4:37 PM
  • thanks for the ideas ;)
    Friday, April 25, 2014 4:47 PM