Public Shared Function GetEnumTypes(ByVal enumName As String) As List(Of EnumObject)
Dim recordset As New List(Of EnumObject)
Dim s As Type = Type.GetType(enumName)
Dim fi As FieldInfo() = s.GetFields
For Each f As FieldInfo In fi
If Not f.IsSpecialName AndAlso Not (f.Name.ToLower = "notset") Then
Dim myValue As Integer = CType(f.GetValue(0), Integer)
recordset.Add(New EnumObject(myValue, f.Name))
End If
Next
Return recordset
End Function
But "s" returns nothing. I am passing in the name of the enumeration. An example below:
Dim enums As List(Of EnumObject) = EnumObject.GetEnumTypes("VehicleStatusType")
For Each en As EnumObject In enums
lbl1.Text &= en.Name
Next
If I understand what you are trying to do, you want to use GetType to load the type of an Enum. Try including the name of the assembly. Here is an example of an assembly named "Test"
public Enum food
pizza
chocolate
End Enum
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
retroviz
Participant
898 Points
249 Posts
Convert the name of a type - to a type
Jan 15, 2009 02:12 PM|LINK
Hi there,
How can I use a string parameter for a method and convert this to a type. I am using reflection to retrieve items within an enumeration:
Dim fi As FieldInfo() = GetType(VehicleContractType).GetFields
Currently I am creatnig a separate method for each enumeration in order to retrieve it's values.
What I would like to do is pass the name of the enumeration as a parameter to the method and cast this as a type.
Any ideas?
Thanks,
Ben
lionscub
Contributor
2561 Points
471 Posts
Re: Convert the name of a type - to a type
Jan 15, 2009 02:22 PM|LINK
Try:
--------------------
http://www.lionsden.co.il
retroviz
Participant
898 Points
249 Posts
Re: Convert the name of a type - to a type
Jan 16, 2009 10:12 AM|LINK
This is what I tried:
Public Shared Function GetEnumTypes(ByVal enumName As String) As List(Of EnumObject) Dim recordset As New List(Of EnumObject) Dim s As Type = Type.GetType(enumName) Dim fi As FieldInfo() = s.GetFields For Each f As FieldInfo In fi If Not f.IsSpecialName AndAlso Not (f.Name.ToLower = "notset") Then Dim myValue As Integer = CType(f.GetValue(0), Integer) recordset.Add(New EnumObject(myValue, f.Name)) End If Next Return recordset End FunctionBut "s" returns nothing. I am passing in the name of the enumeration. An example below:
Thanks,
Ben
lionscub
Contributor
2561 Points
471 Posts
Re: Convert the name of a type - to a type
Jan 18, 2009 08:14 AM|LINK
If I understand what you are trying to do, you want to use GetType to load the type of an Enum. Try including the name of the assembly. Here is an example of an assembly named "Test"
--------------------
http://www.lionsden.co.il
retroviz
Participant
898 Points
249 Posts
Re: Convert the name of a type - to a type
Jan 20, 2009 09:06 AM|LINK
I was not including the namespace.
It works now using Type.GetType so thank you.
Ben