Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I want to access a variable by using the variable name. (in visual basic)

ex)
VB
Dim var1 As Double = 1.0
Dim var2 = ByRef GetVariable("var1")
var2 = 2.0    ' Then var1's value changed 2.0


If not possible, is there any way to use the map structure?

ex)
VB
map["var1"] = ByRef GetVariable("var1")


Please advise.

Thank you.
Posted

1 solution

No, you can't do that.
And I can't imagine a situation where that wouldn't be just confusing and dangerous.
The major problem is that var1 doesn't exist: it's a local, stack based variable which means that it firstly has a limited existence (it will be deallocated when the current method ends, so a reference to it becomes extremely dangerous) and secondly that it doesn't exist by name in the final executable as it will be optimised into the IL stack anyway.

What are you trying to do that you think this is a good idea?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900