Chaffe
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
-------------------------- ---------- ---------- ---------- ---
--------------------------
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
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
try
Catch ex As MissingMemberException
end try
ASKER
Thanks for the help appari
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