Cannot create Enum Type from Type.GetType("EnumName")
-
I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case.
Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
Is there something I am missing? Or does it not work with Enums? Thanks in advance, Dallas -
I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case.
Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
Is there something I am missing? Or does it not work with Enums? Thanks in advance, Dallas[Enum].GetNames(Type.GetType(myEnum))
-
[Enum].GetNames(Type.GetType(myEnum))
Type.GetType(typeName As String) expects a string variable. Besides, I do not know what enum will be thrown to the function so I cannot give it a specific enum declaration.
-
Type.GetType(typeName As String) expects a string variable. Besides, I do not know what enum will be thrown to the function so I cannot give it a specific enum declaration.
Sorry its just getType not type.gettype like this: objectState is a public enum.
Dim tmp As String For Each tmp In [Enum].GetNames(GetType(objectState)) Console.WriteLine(tmp) Next
-
Sorry its just getType not type.gettype like this: objectState is a public enum.
Dim tmp As String For Each tmp In [Enum].GetNames(GetType(objectState)) Console.WriteLine(tmp) Next
I am not refering to the GetType operator, but to the GetType method of the Type class in the System namespace. Just type in System.Type.GetType(...) and intellisense will show you the shared method I am trying to use.
-
I am not refering to the GetType operator, but to the GetType method of the Type class in the System namespace. Just type in System.Type.GetType(...) and intellisense will show you the shared method I am trying to use.
huh, what are you trying to do. I guess I dont see what you want to accomplish. [enum].getNames(gettype(myEnum)) will get the names in the enum [enum].getName(gettype(myEnum, myVar)) will get a name of an enmum variable dim value as myenum will create an instence of the enum (like you said in your first post)
-
I need to create an instance of an enum at runtime, so I had thought the Type.GetType(typeName As String) would be sufficient. However, this seems to be not the case.
Public Function GetEnumNames(ByVal enumName As String) As String() Dim t As Type = Type.GetType(enumName) ' returns nothing ' if it worked, then I could do something like this... Dim names() As String = [Enum].GetNames(t) Return names End Function
Is there something I am missing? Or does it not work with Enums? Thanks in advance, DallasMy bad.... Turns out the enum I was working with was declared within a class (I guess I had to leave for the weekend to figure it out). Once I moved it so it was no longer nested, it worked.