Link to home
Start Free TrialLog in
Avatar of Chaffe
ChaffeFlag for Afghanistan

asked on

pass constant name as a paramenter

Greetings, I have the following code.  in my Test() function, how do I retrive the value of a constant by passing constant name as a string.  (the constant name is the id parameter of the test function)  Thanks.
-----------------------------------------------------------
 Public Class MyClass1
            Public Const Str1 As String = "data1"
            Public Const Str2 As String = "data2"
End Class

 Public Class MyClass2
        Public Function Test(ByVal ID As String) As String
        Dim Keywords As String = ""
        Keywords = MyClass1.ID              'this obviously doesn't work.

        Return Keywords
    End Function

End Class
-----------------------------------------------------------
Avatar of gangwisch
gangwisch

you normally make your constants global inside a module but here is the way i would write it

            Public Const Str1 As String = "data1"
            Public Const Str2 As String = "data2"

 Public Class MyClass1

End Class

 Public Class MyClass2
        Public Function Test(ByVal ID As String) As String
        Dim Keywords As String = ""
        Keywords = MyClass1.ID              'this obviously doesn't work.

        Return Keywords
    End Function
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chaffe

ASKER

appari, this is working great.  Is this going to return "nothing" if it doesn't fine the requested const? I'm just trying to think about exception handling.  Thanks.
throws missingmemberexception
try


Catch ex As MissingMemberException

end try
Avatar of Chaffe

ASKER

Thanks for the help appari