Solved

pass constant name as a paramenter

Posted on 2006-10-26
5
187 Views
Last Modified: 2010-04-23
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
-----------------------------------------------------------
0
Comment
Question by:Chaffe
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:gangwisch
ID: 17816255
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
0
 
LVL 39

Accepted Solution

by:
appari earned 300 total points
ID: 17816270
try like this

Public Function Test(ByVal ID As String) As String
        Dim Keywords As String = ""
        Dim MyClass1 As New Class2
        Keywords = CallByName(MyClass1, ID, CallType.Get)              

        Return Keywords
    End Function
0
 

Author Comment

by:Chaffe
ID: 17816431
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.
0
 
LVL 39

Expert Comment

by:appari
ID: 17816489
throws missingmemberexception
try


Catch ex As MissingMemberException

end try
0
 

Author Comment

by:Chaffe
ID: 17817553
Thanks for the help appari
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question