Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 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 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...