What is the difference between eval, execute and executeglobal.
Eval is used for evaluating the syntax while execute and execute global are used for executing the statements.
Lets talk about the Eval & Execute first.
Eval always take the "=" as comparision
while the execute take the "=" as assignment
Example:
test1
Sub test1
Dim z
z="2"
print eval("z=3") & "--- Eval Print - shuld return False "
print eval("z=2") & "--- Eval Print - shuld return True "
print z & "--- Eval Print - value of z should be unchanged"
print execute ("z=8") & "- should be blank as execute will not return anything"
print z & "--- Eexcute Print - should print 8"
End Sub
Let talk about execute and executeglobal now
execute scope is local while the scope of executeglobal is global. Lets take an example to more clearly understand the same:
'--Execute
Dim x
x="Local"
print x &" - Before Loop"
test2
Sub test2
Dim x
x="abc"
execute"x=" & """in loop"""
print x
End Sub
print x & " - after loop"
This code will print the "Local" before the procedure, "in loop" in the procedure & "Local" again after the procedure as the scope of execute is constant. .
So what the code has done in the procedure: First it assigned the value "abc" to the x and then overwrite this value of x by "in loop"
If we had used the executeglobal instead of execute then we should have received:
Local - before procedure
"abc" in procedure ( as the scope of executeglobal will not effect the value of x in the procedure)
in loop after the procedure

The last section about executeGlobal is wrongly demonstrated.
ReplyDelete