Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstract Enum selection box
    text
    copied!<p>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.</p> <p>This code works but not as well as I'd like:</p> <p>Public Function getEnumSelection(ByVal owner As Windows.Forms.IWin32Window, ByVal sampleValue As [Enum], ByVal subtitle As String) As String</p> <pre><code> 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 </code></pre> <p>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.</p> <p>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)</p> <p>It says sampleValue is not a type. So... how do I get the sampleValue's type to parse?</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload