I'm trying to make a function that can take an enum type, display all the possible selections to the user, let them select one and then pass it back. Generics don't allow you to limit to enum. I've got code working that will cast back and forth, but I'd like it to accept and return the same enum type.
This code works but not as well as I'd like:
Public Function getEnumSelection(ByVal owner As Windows.Forms.IWin32Window, ByVal sampleValue As [Enum], ByVal subtitle As String) As String
Dim names As String() = [Enum].GetNames(sampleValue.GetType)
Using mInput As New dlgList
mInput.ListBox1.Items.Clear()
For Each name As String In names
mInput.ListBox1.Items.Add(name)
Next
mInput.ShowDialog(owner)
Return mInput.ListBox1.SelectedItem.ToString
End Using
End Function
After it runs I can [Enum].parse on the caller directly to the enum type since I have access to it there, but I'd like to eliminate this manual step.
I would like to be able to return the same enum type or at least do the parse back to a value as I receive and cast it in this function but it doesn't seem to allow this line. Dim result As Object = [Enum].Parse(GetType(sampleValue), mInput.ListBox1.SelectedItem.ToString, True)
It says sampleValue is not a type. So... how do I get the sampleValue's type to parse?
Or is there another way to easily and generically allow a user to select an enum value without hand coding a mapping function for each enum?