Variable string to variable

crashrox
10-02-2004, 12:45 AM
Im using VB.net and I have a variable that contains a string which is the name of another variable. I want to access that variable based off of that string.

shaul_ahuva
10-02-2004, 12:56 AM
You can get/set a particular variable by name if it is declared as a member variable inside of a class (not a function) by using Reflection. Of course, in order to access the variable it would need to have the appropriate visibility level (accessing a private field on another class won't work).

Assuming the variable is either in the same class as the class you are trying to access it from or it is a public variable in another class, you can use the System.Type.GetField method to get a FieldInfo object. Then you can use the GetValue/SetValue methods to actually get/set the variable's data.

Here's a link to msdn for System.Type: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypememberstopic.asp

and one for FieldInfo: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemreflectionfieldinfomemberstopic.asp

crashrox
10-02-2004, 01:12 AM
Maybe my first post was a bit misleading... I want to do something like this (in theory)

var1 = "some test"
var2 = "var1"

now based off the value of var2 I want to access var1 's value. So I need to some how convert the value of var2 to a variable.

shaul_ahuva
10-02-2004, 01:37 AM
You mean something along the lines of:


'Retrieves the variable referenced by var2
var3 = GetVariable(var2) 'Not a real function!


Basically, you first need to get the Type of the class containing the variable referenced by var2. I'll assume for now that all of this is occurring in one class:


'Get the type.
Dim t As Type = Me.GetType()


Then, you need to get the FieldInfo class for the variable referenced by var2 (variables are fields):


'Get the field information for the referenced field -- Type.GetField takes the name of the field as a parameter.
Dim fi As FieldInfo = t.GetField(var2)


From here, you can get/set the value of the variable reference by var2 via the newly created FieldInfo object:


'Get the value of the referenced field in Me.
var3 = fi.GetValue(Me)

'Set the value of the referenced field in Me.
fi.SetValue(Me, var3)

vigorniensis
10-02-2004, 02:50 AM
Great thread - very interesting topic. Thanks Shaul.

shaul_ahuva
10-02-2004, 08:57 PM
Great thread - very interesting topic. Thanks Shaul.

Not a problem -- reflection has become my friend :)

crashrox
10-03-2004, 12:32 PM
I tried something along these lines... and I got some errors. var3 = fi.GetValue(Me) produced "An unhandled exception of type 'System.NullReferenceException' occurred in test.exe"

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

_______________________________


Dim var1, var2, var3 As String

var1 = "some text"
var2 = var1

Dim t As Type = Me.GetType()

Dim fi As FieldInfo = t.GetField(var2)

var3 = fi.GetValue(Me)

fi.SetValue(Me, var3)

Console.Write(var3)

shaul_ahuva
10-04-2004, 09:26 AM
The reason you are getting a null reference exception is because you used Dim to declare your variables; you must use public instead. As far as I know, you will only be able to get FieldInfo objects for public variables.

crashrox
10-04-2004, 01:32 PM
wow, thats really annoying. It works as public!! Thank you soooo much! Let me clean up the code and post it... Thanks again!

crashrox
10-04-2004, 01:41 PM
Allright... the final solution! THANKS SHAUL


Public var1, var2 As String 'MUST BE PUBLIC

________________________________________

var1 = "Some Text"
var2 = "var1"


Dim t As Type = Me.GetType()
Dim fi As FieldInfo = t.GetField(var2) 'get the value of var2's string reference
'fi.SetValue(Me, "New Value") 'change the value of var2's string reference

Console.WriteLine(fi.GetValue(Me))

shaul_ahuva
10-04-2004, 02:01 PM
Not a problem. As annoying as it is, it's just a natural result of OOP -- external objects shouldn't be allowed to access internal objects (although I'm kind of fuzzy on how Friend variables aren't considered Fields since they are available to external objects) :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum