Solved

pass constant name as a paramenter

Posted on 2006-10-26
5
185 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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 …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now