Results 1 to 14 of 14

Thread: [RESOLVED] How to pass enum type to a function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Resolved [RESOLVED] How to pass enum type to a function

    I have a need to retrieve the name associated with a given value in a particular enum (the enum is named FormMode). I can retrieve the name using this code:

    [Enum].GetName(GetType(FormMode), enum_item_value)

    This works but the syntax strikes me as a little cumbersome, so I wrote a wrapper function to make my code easier to understand:

    Public Function GetItemName(ItemValue As Int32) As String
    Return [Enum].GetName(GetType(FormMode), ItemValue)
    End Function

    This also works, but now I want to generalize the function so that it will do the same job for ANY enum. I've tried this:

    Public Function GetItemName2(EN As System.Enum, ItemValue As Int32) As String
    Return [Enum].GetName(GetType(EN), ItemValue)
    End Function

    but it produces the error "Type 'EN' is not defined." with EN underlined in red in the RETURN statement. I get the same error when I change the parameter type from System.Enum to System.Type.

    How can I pass the specific enum as a parameter in such a way that it can be used by the GetType method?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to pass enum type to a function

    First things first, please don;t post unformatted code snippets. Here's how your post should look:
    Quote Originally Posted by silverblatt View Post
    I have a need to retrieve the name associated with a given value in a particular enum (the enum is named FormMode). I can retrieve the name using this code:

    Code:
    [Enum].GetName(GetType(FormMode), enum_item_value)
    This works but the syntax strikes me as a little cumbersome, so I wrote a wrapper function to make my code easier to understand:

    Code:
        Public Function GetItemName(ItemValue As Int32) As String
            Return [Enum].GetName(GetType(FormMode), ItemValue)
        End Function
    This also works, but now I want to generalize the function so that it will do the same job for ANY enum. I've tried this:

    Code:
        Public Function GetItemName2(EN As System.Enum, ItemValue As Int32) As String
            Return [Enum].GetName(GetType(EN), ItemValue)
        End Function
    but it produces the error "Type 'EN' is not defined." with EN underlined in red in the RETURN statement. I get the same error when I change the parameter type from System.Enum to System.Type.

    How can I pass the specific enum as a parameter in such a way that it can be used by the GetType method?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to pass enum type to a function

    As for the issue, there actually is no issue. Just forget everything you just posted and do this instead:
    vb.net Code:
    1. enum_item_value.ToString()
    Here's an example that shows that it produces the same result, whether the value is boxed or not:
    vb.net Code:
    1. Dim x = Environment.SpecialFolder.System
    2.  
    3. Console.WriteLine([Enum].GetName(GetType(Environment.SpecialFolder), x))
    4. Console.WriteLine(x.ToString())
    5.  
    6. Dim y As Object = x
    7.  
    8. Console.WriteLine([Enum].GetName(GetType(Environment.SpecialFolder), y))
    9. Console.WriteLine(y.ToString())

    EDIT:

    I'll leave the post above for completeness but I realised that you're actually dealing with Integer values and not the Enum values themselves, so ToString is not helpful on its own in that case. You'd have to cast as the desired type first, e.g.
    vb.net Code:
    1. DirectCast(enum_item_value, FormMode).ToString()

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to pass enum type to a function

    This works...

    Code:
    Private Enum testEnum
        red = 0
        green = 1
        blue = 2
    End Enum
        
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        MsgBox(GetItemName2("testEnum", 2))
    End Sub
    
    Public Function GetItemName2(ByVal EN As String, ByVal ItemValue As Int32) As String
        Dim exAssembly = Reflection.Assembly.GetExecutingAssembly
        Dim enumType = exAssembly.GetTypes.First(Function(f) f.Name = EN)
        Return [Enum].GetName(enumType, ItemValue)
    End Function

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to pass enum type to a function

    As for why what you were trying to do couldn't work, the GetType operator that you're using expects a data type. That means that you have to provide something that could also be used after As in the definition of a variable. In your first code snippet:
    vb.net Code:
    1. [Enum].GetName(GetType(FormMode), enum_item_value)
    you're specifying the FormMode data type. It's obviously a data type because you could do this:
    vb.net Code:
    1. Dim var As FormMode
    Now look at your third code snippet:
    vb.net Code:
    1. Public Function GetItemName2(EN As System.Enum, ItemValue As Int32) As String
    2.     Return [Enum].GetName(GetType(EN), ItemValue)
    3. End Function
    You're trying to pass EN to GetType. Is EN a data type? Could you do this:
    vb.net Code:
    1. Dim var As EN
    Obviously not.

    Many people run into this kind of issue before they fully understand data types and how they relate to objects, myself included. Generics can often help us in situations like this. Generics allow you to declare a method that basically takes a data type as a parameter. In your case, you could do this:
    vb.net Code:
    1. Public Function GetItemName2(Of TEnum)(ItemValue As Int32) As String
    2.     Return [Enum].GetName(GetType(TEnum), ItemValue)
    3. End Function
    and then call that method like this:
    vb.net Code:
    1. Dim fmName = GetItemName2(Of FormMode)(enum_item_value)
    Note that this will accept any data, including those that are not Enum types. You can limit it to value types but there's no way to limit it to just Enums. Just be sure to specify a valid type or an exception will be thrown. Of course, you'd have to do the same if you were just using your original code anyway.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to pass enum type to a function

    Quote Originally Posted by .paul. View Post
    This works...

    Code:
    Private Enum testEnum
        red = 0
        green = 1
        blue = 2
    End Enum
        
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        MsgBox(GetItemName2("testEnum", 2))
    End Sub
    
    Public Function GetItemName2(ByVal EN As String, ByVal ItemValue As Int32) As String
        Dim exAssembly = Reflection.Assembly.GetExecutingAssembly
        Dim enumType = exAssembly.GetTypes.First(Function(f) f.Name = EN)
        Return [Enum].GetName(enumType, ItemValue)
    End Function
    The OP indicates that the type is actually known, rather than stored as a String. In that case, a generic method would be preferable to converting the type to a String first and then using that to get a Type object.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: How to pass enum type to a function

    Thanks, jmcilhinney and .paul. The generic function does the trick for me. And, as jmcilhinney suggested, I've added error handling.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to pass enum type to a function

    Looking at the very first code snippet you posted:
    vb.net Code:
    1. [Enum].GetName(GetType(FormMode), enum_item_value)
    what is the data type of enum_item_value? If it is Integer then the generic method is worthwhile. If not, you should just use the ToString option that I suggested in the first place and do away with all this complexity.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: [RESOLVED] How to pass enum type to a function

    The enum's member's values are integers.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to pass enum type to a function

    Quote Originally Posted by silverblatt View Post
    The enum's member's values are integers.
    That doesn't matter. What matters is the type of the enum_item_value variable. If it is type FormMode then calling its ToString method will return the name of the enumerated value. If it's Integer then it will be treated like any other number.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: [RESOLVED] How to pass enum type to a function

    enum_item_value is of type FormMode.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to pass enum type to a function

    Quote Originally Posted by silverblatt View Post
    enum_item_value is of type FormMode.
    Then you should just do as I said initially and call ToString on it and forget everything else.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: [RESOLVED] How to pass enum type to a function

    That's exactly what I did in the end. But it's been an educational journey through generic functions and type handling, so I'm glad for it. Thanks again.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to pass enum type to a function

    Quote Originally Posted by silverblatt View Post
    But it's been an educational journey through generic functions and type handling, so I'm glad for it.
    Yes, don't literally forget everything else. Just don't overcomplicate this specific scenario with that stuff. It's bound to come in useful in the future though.

Tags for this Thread

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