views:

142

answers:

1

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?

+1  A: 

To answer the smallest question first, you get the type of an object by calling sampleValue.GetType(), just like you're already doing in the first line of your function. GetType is both a keyword and a method of the Object class - the first gets the type of a type (somewhat tautologically) and the second gets the type of an object instance.

As for the larger question, I would suggest using a generic method with a slightly more relaxed constraint on the parameters: let it accept any structure, not just enums. You lose a little bit of type safety, but I would think that's an OK tradeoff. If someone passes in a non-enum structure, they would get an ArgumentException at runtime, so it's not like you'd get garbage out of the function.

Public Function getEnumSelection(Of T As Structure)(ByVal owner As Windows.Forms.IWin32Window, ByVal subtitle As String) As T
    Dim names As String() = [Enum].GetNames(GetType(T))
    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 DirectCast([Enum].Parse(GetType(T), mInput.ListBox1.SelectedItem.ToString), T)
    End Using
End Function
Helen Toomik
I like it with the following changes Return DirectCast([Enum].Parse(GetType(T), mInput.ListBox1.SelectedItem.ToString), T)and Public Function getEnumSelection(Of T As Structure)(ByVal owner As Windows.Forms.IWin32Window, ByVal subtitle As String) As T
Maslow
Yes, sorry, that was what I meant - sloppy copy-pasting on my part. I will edit the post.
Helen Toomik