Define a new variable by concatenation of two other variable names in VBS

不羁的心 提交于 2019-12-13 04:27:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!