Wednesday, 30 November 2011

Difference b/w Eval & Execute

Eval & Execute Statement
Eval: Evaluates an expression and returns the result. (True (-1)  & False (0))
Required. String containing any legal VBScript expression
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 Eval method always uses the second interpretation, whereas the Execute statement always uses the first.
Note In Microsoft® JScript™, no confusion exists between assignment and comparison, because the assignment operator (=) is different from the comparison operator (==).
The following example illustrates the use of the Eval function: 
Sub GuessANumber
   Dim Guess, RndNum
   RndNum = Int((100) * Rnd(1) + 1)
   Guess = CInt(InputBox(“Enter your guess:”,,0))
   Do
      If Eval(“Guess = RndNum”) Then
         MsgBox “Congratulations! You guessed it!”
         Exit Sub
      Else
         Guess = CInt(InputBox(“Sorry! Try again.”,,0))
      End If
   Loop Until Guess = 0
End Sub
Execute: Executes one or more specified statements
The required statement argument is a string expression containing one or more statements for execution. Include multiple statements in the statement argument, using colons or embedded line breaks to separate them. 
The context in which the Execute statement is invoked determines what objects and variables are available to the code being run. In-scope objects and variables are available to code running in an Execute statement. However, it is important to understand that if you execute code that creates a procedure, that procedure does not inherit the scope of the procedure in which it occurred.
Like any procedure, the new procedure’s scope is global, and it inherits everything in the global scope. Unlike any other procedure, its context is not global scope, so it can only be executed in the context of the procedure where the Execute statement occurred. However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global. The following example illustrates this behavior:
Dim X   ‘ Declare X in global scope.
X = “Global”   ‘ Assign global X a value.
Sub Proc1   ‘ Declare procedure.
   Dim X   ‘ Declare X in local scope.
   X = “Local”   ‘ Assign local X a value.
            ’ The Execute statement here creates a
            ’ procedure that, when invoked, prints X.
            ’ It print the global X because Proc2
            ’ inherits everything in global scope.
   Execute “Sub Proc2: Print X: End Sub”
   Print Eval(“X”)   ‘ Print local X.
   Proc2   ‘ Invoke Proc2 in Proc1′s scope.
End Sub
Proc2   ‘ This line causes an error since 
        ’ Proc2 is unavailable outside Proc1.
Proc1   ‘ Invoke Proc1.
   Execute “Sub Proc2: Print X: End Sub”
Proc2   ‘ This invocation succeeds because Proc2
        ’ is now available globally

No comments:

Post a Comment