Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get Reference to a Varible by Name (String)

Posted on 2008-06-12
6
Medium Priority
?
392 Views
Last Modified: 2013-11-26
VS 2008
I have a form that has a combobox with the list of variable names available to the user. The user selects the one he needs and with that name, I need to return or show the value in that Variable. Here is an abstract sample of what I need.

Public class Test1
  Private mvar1 as String = "Value 1"
  Private mvar2 as decimal = 23.54
  Private oview as DataView = New Dataview()
 
  Public function GetVarValue(byval VarName as String) as Object
     dim ReturnVal as Object = Nothing

     ' do something here to get the value. This is what I need
    '  ReturnVal = GetVarRef(VarName).Text
    '  ReturnVal = GetViewRef(VarName).rows(0)("field1")
    return ReturnVal
End Function
End Class


From Another Class I want to call that Function and Get the Object Value Like
Dim Obj as Object = Test1.GetVarValue("mvar1")

0
Comment
Question by:ofern01
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 21771919
So something like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t1 As New Test1
        MessageBox.Show(t1.GetVarValue("mvar1"))
        MessageBox.Show(t1.GetVarValue("mvar2"))
    End Sub

    Public Class Test1

        Private mvar1 As String = "Value 1"
        Private mvar2 As Decimal = 23.54

        Public Function GetVarValue(ByVal VarName As String) As Object
            Try
                Dim fi As System.Reflection.FieldInfo = Me.GetType.GetField(VarName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
                Return fi.GetValue(Me)
            Catch ex As Exception
                Return Nothing
            End Try            
        End Function

    End Class
0
 
LVL 18

Expert Comment

by:Priest04
ID: 21772164
It is not possible to get the name of the local variable, since its name is not compiled into assembly's metadata, therefore it is not available at runtime.

You may want to try some alternative method, like having a collection that will based on the key (variable name entered by user) retrieve varable value. This would mean that you would need to hardcode adding an element in collection for each variable you have declared.

Goran
0
 
LVL 18

Expert Comment

by:Priest04
ID: 21772180
If it is a class field, like in Idle_Mind example, then it is possible, but with local variables, not possible.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:ofern01
ID: 21774429
Idle Mind, That will resolve the Variable Values, Thank you, but what about the view. For this I need to return a reference so that I can Manipulate the view info. The view can also be a DataTable.

Priest04, I do not undertand you comment about class field or variables. As far as I know, Idle Mind is using variables, if not, what is the diference?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 21774659
You can use it for whatever you want...just make sure you have the right type and cast it:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t1 As New Test1
        Dim o As Object = t1.GetVarValue("oview")
        If Not (o Is Nothing) Then
            If TypeOf o Is DataView Then
                Dim dv As DataView = DirectCast(o, DataView)
                 ' ...do something with "dv"...
            End If
        End If
    End Sub
 
    Public Class Test1
 
        Private mvar1 As String = "Value 1"
        Private mvar2 As Decimal = 23.54
        Private oview As DataView = New DataView()
 
        Public Function GetVarValue(ByVal VarName As String) As Object
            Try
                Dim fi As System.Reflection.FieldInfo = Me.GetType.GetField(VarName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
                Return fi.GetValue(Me)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
 
    End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:ofern01
ID: 31466534
Thanks for you fast and accruate response
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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