Below is a code segment that would execute your intended behavior. However,
the nature of this application is difficult to maintain. It also requires
reflection security clearance.
I should note that this application could be easily rewritten using ByRef,
but I would assume that based on your response you are dynamically building
the variable name at runtime (For example loading the var names from a file
or some such runtime only approach.) Keep in mind - reflection is not for
the squeamish. You really shouldn't just jump into it without first fully
understanding the nature of the problem. And when using it to override the
normal variable access methods, understand that it is VERY slow and
difficult to debug. Only consider using reflection this way as a last
resort.
----------
Public Class DemonstrationClass
'Declare the variables we want to be able to change.
Public FirstVar As Integer
Public SecondVar As Integer
Public ThirdVar As Integer
Public Sub AlterVar(ByVal Multiplier As Integer, ByVal VarName As
String)
'Get a reference to the variable we want to change
Dim TargetFieldInfo As Reflection.FieldInfo =
Me.GetType.GetField(VarName)
Dim OldValue As Integer
Dim NewValue As Integer
'Get the old value from ourselves (me) and cast it from Object to
Integer
OldValue = CType(TargetFieldInfo.GetValue(Me), Integer)
'Calculate the new value.
NewValue = OldValue * Multiplier
'And then reassign the new value to our instance of
DemonstrationClass.
TargetFieldInfo.SetValue(Me, NewValue)
End Sub
Public Sub Go()
Dim TargetName As String
Dim tmpMultString As String
Dim tmpMultiplier As Integer
'Initalize our three variables.
FirstVar = 10
SecondVar = 20
ThirdVar = -10
'Display the current state.
Console.Out.WriteLine("Before AlterVar")
Console.Out.WriteLine("FirstVar: " + FirstVar.ToString)
Console.Out.WriteLine("SecondVar: " + SecondVar.ToString)
Console.Out.WriteLine("ThirdVar: " + ThirdVar.ToString)
Console.Out.WriteLine()
'Prompt the user for the var name.
Console.Out.Write("Which Var do you want to change? ")
TargetName = Console.In.ReadLine()
'Prompt the user for the multiplier.
Console.Out.Write("What do you want to multiply it by? ")
tmpMultString = Console.In.ReadLine()
'And get the integer value of what they entered
tmpMultiplier = Integer.Parse(tmpMultString)
'Call our routine.
AlterVar(tmpMultiplier, TargetName)
'And redisplay the state.
Console.Out.WriteLine("After AlterVar")
Console.Out.WriteLine("FirstVar: " + FirstVar.ToString)
Console.Out.WriteLine("SecondVar: " + SecondVar.ToString)
Console.Out.WriteLine("ThirdVar: " + ThirdVar.ToString)
Console.Out.WriteLine()
'Get one char from the buffer (Which won't be populated till the
user presses anyway.)
Console.Out.WriteLine("Press Enter to Continue.")
Console.In.Read()
End Sub
Public Shared Sub Main()
Dim MyDemoClass As New DemonstrationClass
MyDemoClass.Go()
MyDemoClass = Nothing
End Sub
End Class
----------
Below is a sample run of the program (remember, spelling and case are
IMPORTANT):
----------
Before AlterVar
FirstVar: 10
SecondVar: 20
ThirdVar: -10
Which Var do you want to change? FirstVar
What do you want to multiply it by? 22
After AlterVar
FirstVar: 220
SecondVar: 20
ThirdVar: -10
Press Enter to Continue.
----------
Hope this helps. ^.^
--
Signed,
John-Michael O'Brien
Student, Urban Drainage and Flood Control District
Blaxer said:
Wow, thank you for such a thorough reply, unfortunatly I cannot see how
this helps me. Maybe I am not explaining what I need in enough detail or
maybe it is not possible to do this...?
To answer your question, yes the variables (there are 3 of them) are
available to the entire class (global). All I need to be able to do is pass
the name of the variable as a string to a function so that it's value can be
updated. Is that possible?