Resolved How to pass enum type to a function

silverblatt

New member
Joined
Sep 28, 2021
Messages
4
Programming Experience
10+
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:

VB.NET:
[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:

VB.NET:
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:

VB.NET:
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?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
You can cast an Integer as an Enum type, so you could just use DirectCast(enum_item_value, FormMode).ToString()

If you really want to go with your own method then you need an actual data type to pass to GetType. The correct way to do that would be to make your method generic:
VB.NET:
Public Function GetItemName(Of TEnum)(ItemValue As Int32) As String
    Return [Enum].GetName(GetType(TEnum), ItemValue)
End Function
TEnum is now an actual data type, i.e. something that you could declare a variable as, e.g.
VB.NET:
Dim var As TEnum
so it is valid to be passed to GetType. You would then call that method like so:
VB.NET:
Dim formModeName = GetItemName(Of FormMode)(enum_item_value)
The main issue with this method is that it could be called with any data type at all, so including those that are not Enum types. That would cause an exception to be thrown so, if an invalid type is a possibility, you should handle that exception. You'd have to do that with your original code anyway.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
Is generics really needed here?
VB.NET:
Public Function GetItemName(value As [Enum]) As String
    Return [Enum].GetName(value.GetType, value)
End Function
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
Is generics really needed here?
VB.NET:
Public Function GetItemName(value As [Enum]) As String
    Return [Enum].GetName(value.GetType, value)
End Function
If the original value is an Integer then yes. I surmised that that was the case from the type of the ItemValue parameter. If the OP is saying that both approaches work then I must assume that the original enum_item_value is actually an Enum type and not an Integer. In that case, we can go back to the idea that I originally had and simply use enum_item_value.ToString(), so no custom methods are required at all.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
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.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
Yes of course, besides ToString will also work for combined flags enum values, GetName will not.
 
Top Bottom