How can I refer to variable by a string?
How can I refer to variable by a string?
Instead of calling a subroutine by
changeValue(testClass.a)
changeValue(testClass.d)
changeValue(testClass.b)
I want to tell the subroutine the respective variable by a string:
"Change the variable <var> of testClass with <var> as String" (or just testClass.<var>)
I already tried the TypeConverter-Class but without much success (I finally ended up with an error "System.NotSupportedException, TypeConverter cannot convert from System.String")
Here a simplified code to illustrate my problem (the actual problem is more complex, so I reduced it to the core problem):
Assume I have class "testClass" with some variables:
Dim a As Integer = 1
Dim b As Integer = 8
Dim c As Integer = 5
Dim d As Integer = 4
Dim e As Integer = 6
I also have an array with all variables of this class I want to use:
Dim varList(2) as String
varList(0)="a"
varList(1)="d"
varList(2)="b"
Now I want to cycle through all varaibles in varList() and change their values:
For i = 0 to varList.GetUpperBound
Call changeValue(varList(i)) ***
Next i
Sub changeValue(ByRef var As Integer) ***
var+=10
End Sub
Finally, this should result in
a=11 b=18 c=5 d=14 e=6
*** Of course I know that I can't hand-over a String to an Integer, it's just to indicate the problem