Wednesday, July 15, 2015

Eval & Execute

 

Difference between Eval & Execute

In the syntax above, both expression and statement arguments are string statements. Both expressions can be derived from VBScript or QTP code; the difference lies in the fact that Eval will always return the result of the string evaluation whereas Execute will execute a string statement for execution and will not retrieve the result (but there are workarounds). The following example demonstrates the main difference between the 2 functions:
x = 9
y = 10
 
bIsEqual = Eval("x = y")
Execute "x = y"
 
MsgBox "bIsEqual: " & bIsEqual
MsgBox "X is no longer 9. It is: " & x
bIsEqual
Eval Function
New x
Execute Statement
Note that Eval returned a Boolean value whereas Execute function performed a variable assignment. This article will cover the usage of both techniques in depth through both VBS and QTP code.
You must be wondering why the syntax for Eval and Execute is different. The reason is that, Eval is a function. Execute is not. That means, the following statement for Eval: bIsEqual = Eval("x = y"), when written for Execute will return a Null value for bResult (but assignment will be performed for x):
bResult = Execute("x = y")
MsgBox bResult
MsgBox "x: " & x
Null Output
Null Output
Value Assigned
X = 10
With that said, let me share the workaround that can be used with Execute to return the evaluation just like we did with Eval. Here’s how:
x = 9
y = 10
 
Execute "z = (x = y)"
MsgBox "z: " & z   ' how come its evaluating the 
Return Z (False)
Return Z (False)
More on this concept will be covered in the section Evaluating Statements.

No comments:

Post a Comment