ofern01
asked on
How to get Reference to a Varible by Name (String)
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")
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
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")
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
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
If it is a class field, like in Idle_Mind example, then it is possible, but with local variables, not possible.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for you fast and accruate response
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.GetVarV
MessageBox.Show(t1.GetVarV
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.FieldInf
Return fi.GetValue(Me)
Catch ex As Exception
Return Nothing
End Try
End Function
End Class