Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

pass constant name as a paramenter

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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