Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

vbscript Eval a string to a Variable in a loop?

I am trying to use vbscript's Eval (or maybe I need Execute) to create some variables from the key names from an ini file. The ini file can have unlimited unknown key=val pairs. I need to create a variable based on the key name no matter what.

Ini File contents:

myPath=c:est
myExe=myapp.exe
....
xxx=123
yyy=abc

My code that reads the ini and returns the key and values to an object

The code I am trying to get working is here:

For each pair in objINI
    Eval("pair.key=pair.val")
Next

msgbox myPath
msgbox myExe

But both msgbox's are showing empty And yes I am sure pair.key and pair.val have the correct values.

Thoughts on what I am missing or if this is even possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to Execute (an assign statement), not to Eval(uate a boolean expression):

>> n = "Name"
>> v = "Value"
>> WScript.Echo TypeName(Eval("n=v"))
>>
Boolean
>> Execute "n=v"
>> WScript.Echo n
>>
Value
>>

From the docs:

In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The Execute statement always uses the first interpretation, whereas the Eval method always uses the second.

(This does not mean you should do such things; neither at home, nor at work)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...