0

I want to refer to an enum by the string name of its root. Please note, i wish to refer to the enum, not an enum member.

There are many posts on stackoverflow describing how to refer an enum member by its name (eg How to retrieve an Enum member given its name), but i didn't find any about how to refer to the enum by the name of it's root.

To further clarify;

Enum MyEnumA : Quiet : Noisy : End enum
Enum MyEnumB : Big : Small : Gigantic : End enum

Sub Foo(strAction as string)
    ' Depending on value of strAction, i want to create a list of either MyEnumA or MyEnumB members
    ' I know i can't do the following, it's just to make clear the direction i'm wanting to go -
    Dim lstMembers As New List(Of CType(strAction,[Enum]))   
    '....
end function

Following the good suggestions below, i've tried this;

Dim enumType As Type = System.Type.GetType("ExcelInterface.clsBTAnalyseRslts+" & "strAction")
Dim lstFldIndx As New List(Of enumtype)   'Fails to compile this line as doesn't recognize enumtype as defined

Thank you!

Community
  • 1
  • 1
Yugmorf
  • 320
  • 1
  • 6
  • 20
  • What exactly do you want in your list? The NAME of each value in "EnumXXX", or each of the VALUES of "EnumXXX". In the first case, you'd have a List(Of String) and a List(Of Integer) for the second case. – igrimpe Nov 23 '12 at 09:50
  • So it seems. Not through bad intention, rather than just not knowing the system. Thanks for pointing out - will look to rectify. – Yugmorf Nov 23 '12 at 17:07

3 Answers3

1

Give this a go, it creates an array so you can change it to a List(Of x...) later:

Sub Foo(ByVal strAction As String)

    Dim exAssembly = Reflection.Assembly.GetExecutingAssembly

    Dim enumType = exAssembly.GetTypes.First(Function(f) f.Name = strAction)

    Dim myEnum = [Enum].GetValues(enumType)

End Sub

I found an interesting post here which gave me some direction.

usage:

Foo("MyEnumA")

I will leave you to do some error handling and checking :D

Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
  • That almost gets me there, but i can't quite make it work. this works: Dim enumType As Type = Reflection.Assembly.GetExecutingAssembly.GetTypes.First(Function(f) f.Name = "Fname") but then i get a compilation error if i do List(of EnumType) or if i do GetType(EnumType). Error says that EnumType is not defined, which is clearly not the case. – Yugmorf Nov 23 '12 at 17:02
  • Keep the function as it is and on the last line, Dim myEnum = [Enum].GetValues(enumType), put a for each loop beneath it and put each item from myEnum into a list(Of String) – Ric Nov 23 '12 at 18:41
1

Here's a code block that converts the enum into a string and then gets the type based on the string. It will show you how to get the type and how you should format the string. The rest should be pretty straight forward.

Dim obj As MyEnumA
Dim t As Type = obj.GetType()
Dim s As String = t.FullName
Dim t2 As Type = System.Type.GetType(s)

Then do this to get the values:

Dim Members() As String
Members = System.Enum.GetNames(t2)

Variable s will look something like this "namespace.class + MyEnumA" so all you need to do is to create this string programmatically and send it to a function.

WozzeC
  • 2,630
  • 1
  • 13
  • 12
  • I believe the OP wants to pass a string eg "MyEnumA" or any other enum as a string and then get the values from each member of the enum. I could be wrong though – Ric Nov 23 '12 at 10:32
  • I agree. For the solution to OPs question you simply use the last row: Dim t2 as Type = System.Type.GetType(strAction). But for this to work strAction needs to be the exact name of the Enum type, which the first three lines supply. All he needs to do now is to get the name, have a look at it and then create his string from that. He wont need line 1-3 in his final solution, it's merely a help on the way. – WozzeC Nov 23 '12 at 11:49
  • Try my solution: Create an enum, copy my function and view the results in the debugger. You should be able to see the enum members on the line Dim myEnum = .... – Ric Nov 23 '12 at 11:51
  • Oh it works dont get me wrong, I'm not saying your solution is bad. I just found the solution looking overly complicated :) But you get extra credit since yours is actually working with "MyEnumA" wheres my function needs "namespace.class+MyEnumA". – WozzeC Nov 23 '12 at 12:07
  • @WozzeC Thanks your suggestion. I did try it and similar to Ric's suggestion, for some reason it doesn't recognize t2 (or EnumType). eg i can do GetType(MyEnumA) but i can't do GetType(t2). similarly for creating a list. – Yugmorf Nov 23 '12 at 17:36
0

Ric's answer didn't quite work for me because my Enum wasn't in the same location as my code, so here's how I finally managed it (I use VB, but I'll add C# for reference):

VB

Dim EnumName As String = "MyEnum"
Dim MyAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(My.Assembly.Location))
Dim MyEnum As System.Type = MyAssembly.GetType("My.Assembly.Location+ClassName+" & EnumName)
Dim EnumNames As String() = MyEnum.GetEnumNames()
For intVal As Integer = 0 To EnumNames.Length - 1
    ' EnumNames(intVal) = Name of Enum Value
    ' intVal = Enum Value
Next

C#

string EnumName = "MyEnum";
System.Reflection.Assembly MyAssembly = System.Reflection.Assembly.GetAssembly(typeof(My.Assembly.Location));
System.Type MyEnum = MyAssembly.GetType("My.Assembly.Location+ClassName+" + EnumName);
string[] EnumNames = MyEnum.GetEnumNames();
for (int intVal = 0; intVal <= EnumNames.Length - 1; intVal++) {
    // EnumNames(intVal) = Name of Enum Value
    // intVal = Enum Value
}

Hopefully it will save someone's valuable time :).

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64