-
Dec 2nd, 2021, 07:09 PM
#1
Thread Starter
Lively Member
[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?
-
Dec 2nd, 2021, 08:29 PM
#2
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:
Originally Posted by silverblatt
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?
-
Dec 2nd, 2021, 08:34 PM
#3
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:
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:
Dim x = Environment.SpecialFolder.System
Console.WriteLine([Enum].GetName(GetType(Environment.SpecialFolder), x))
Console.WriteLine(x.ToString())
Dim y As Object = x
Console.WriteLine([Enum].GetName(GetType(Environment.SpecialFolder), y))
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:
DirectCast(enum_item_value, FormMode).ToString()
Last edited by jmcilhinney; Dec 2nd, 2021 at 08:44 PM.
-
Dec 2nd, 2021, 08:42 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 2nd, 2021, 08:42 PM
#5
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:
[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:
Now look at your third code snippet:
vb.net Code:
Public Function GetItemName2(EN As System.Enum, ItemValue As Int32) As String
Return [Enum].GetName(GetType(EN), ItemValue)
End Function
You're trying to pass EN to GetType. Is EN a data type? Could you do this:
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:
Public Function GetItemName2(Of TEnum)(ItemValue As Int32) As String
Return [Enum].GetName(GetType(TEnum), ItemValue)
End Function
and then call that method like this:
vb.net Code:
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.
Last edited by jmcilhinney; Dec 2nd, 2021 at 08:58 PM.
-
Dec 2nd, 2021, 08:46 PM
#6
Re: How to pass enum type to a function
Originally Posted by .paul.
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.
-
Dec 3rd, 2021, 01:15 PM
#7
Thread Starter
Lively Member
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.
-
Dec 3rd, 2021, 09:18 PM
#8
Re: [RESOLVED] How to pass enum type to a function
Looking at the very first code snippet you posted:
vb.net Code:
[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.
-
Dec 3rd, 2021, 10:21 PM
#9
Thread Starter
Lively Member
Re: [RESOLVED] How to pass enum type to a function
The enum's member's values are integers.
-
Dec 4th, 2021, 02:01 AM
#10
Re: [RESOLVED] How to pass enum type to a function
Originally Posted by silverblatt
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.
-
Dec 6th, 2021, 12:35 PM
#11
Thread Starter
Lively Member
Re: [RESOLVED] How to pass enum type to a function
enum_item_value is of type FormMode.
-
Dec 6th, 2021, 07:12 PM
#12
Re: [RESOLVED] How to pass enum type to a function
Originally Posted by silverblatt
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.
-
Dec 6th, 2021, 11:05 PM
#13
Thread Starter
Lively Member
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.
-
Dec 6th, 2021, 11:25 PM
#14
Re: [RESOLVED] How to pass enum type to a function
Originally Posted by silverblatt
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|