In JavaScript, I was able to access variables by using the variable name in a string in the "window" and "document" objects:
window["var_name"]
This was very useful, because I could make variable changes dynamically as long as they were members of the window or document objects.
I was wondering if there was a way to do this in VBScript, and what this process was called开发者_运维问答.
There's nothing strictly equivalent.
If you want to dynamically access variables, you can use the Execute statement, which is similar to eval in other languages.
Dim x, var_name
var_name = "x"
Execute var_name & " = 10"
WScript.Echo x '' Prints 10
If you want to get a reference to a Sub or Function, you can use GetRef.
Dim x
Set x = GetRef("my_sub")
x
Sub my_sub
WScript.Echo "Hello, world"
End Sub
精彩评论