Solved

pass constant name as a paramenter

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

22 Experts available now in Live!

Get 1:1 Help Now