问题
I am writing a long script for two weeks ago and now due to the way whose it's written, I need to define a variable name by concatenation of two existing variable names. I don't want to concatenate their values but the variables themselves in order to make a new variable. That may sound ridicule, but given the situation, I have no other choice.
Is it possible?
Thank you for your understanding.
回答1:
VBScript doesn't support introspection, so you can't dynamically resolve variable names from within the script. However, there might be a possible way to go about. If instead of variables a and b you use a dictionary with keys "a" and "b" you could concatenate those key names to produce a new key "ab":
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", 23
d.Add "b", 42
newkey = ""
For Each key In d.Keys
newkey = newkey & key
Next
d.Add newkey, 2342
However, you might be able to get a better answer if you described the actual problem you're trying to solve instead of what you perceive as the solution.
回答2:
1.String together multiple variables (a and b) into one variable called strFullName.
a.strFirstName = “Test”
b.strLastName = “Automation”
Use concatenation, so that the variable strFullName is equal to “Automation,Test” using variables from a and b listed above.
来源:https://stackoverflow.com/questions/16650321/define-a-new-variable-by-concatenation-of-two-other-variable-names-in-vbs