1

I have a class in VB with some constants that represent the string name of my security roles. I need to be able to call a function to return to me a string array(or collection, or whatever) of the values of each of these constants. I will be using it to make sure that my databases Roles table has the same roles as coded into the application.

Public Class Roles
    Public Const Administrator = "Administrator"
    Public Const BasicUser = "Basic User"
    Public Const PowerUser = "Power User"
End Class

I'm looking to run a function, i.e. ClassConstantsToStringArray(gettype(Roles)) that will return to me "Administrator","Basic User","Power User"

I'm know reflection is the way to go, I just don't know enough about using it yet to get what I want. I found a function on the net that would return to me the constant names in a FieldInfo array but still don't have enough smarts to make that work for me.

Thanks.

Dennis
  • 179
  • 1
  • 2
  • 9

2 Answers2

3

Ok, here's how it is done. I didn't do this on my own though, http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx had the answer, I just ran it through a C# to VB.Net converter.

Function GetStringsFromClassConstants(ByVal type As System.Type) As String()
    Dim constants As New ArrayList()
    Dim fieldInfos As FieldInfo() = type.GetFields(BindingFlags.[Public] Or _ 
        BindingFlags.[Static] Or BindingFlags.FlattenHierarchy)

    For Each fi As FieldInfo In fieldInfos
        If fi.IsLiteral AndAlso Not fi.IsInitOnly Then
            constants.Add(fi)
        End If
    Next

    Dim ConstantsStringArray as New System.Collections.Specialized.StringCollection

    For Each fi as FieldInfo in _ 
        DirectCast(constants.ToArray(GetType(FieldInfo)), FieldInfo())
        ConstantsStringArray.Add(fi.GetValue(Nothing))
    Next

    Dim retVal(ConstantsStringArray.Count - 1) as String
    ConstantsStringArray.CopyTo(retval,0)
    Return retval    
End Function
Dennis
  • 179
  • 1
  • 2
  • 9
2

You can build an object that is nearly identical to a string enum in VB.Net. See my previous post on the topic here:

Getting static field values of a type using reflection

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I liked this solution also but since I was 99% of the way there, I just stuck with what I was working with. +1 though. – Dennis Sep 21 '09 at 20:41