When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Friday, March 03, 2000

Using the Eval and Execute Functions

VBScript provides a powerful function to evaluate string expressions as though they had been typed in directly by the programmer. This allows VBScripts to perform dynamic evalutations. For example, the user can enter a mathematical formula - 4 + 5 * 9, for example - and have the numerical result spit back using the Eval function.

- continued -

Imagine that you wanted to be able to parse a user input, like 4 + 5 * 9, without using the Eval function. What would you have to do? For starters, you would have to iterate through the user's input string, picking it apart, which would be a pain. Not only that, but you would need to be mindful of order of operations! What if the user placed a pair of parenthesis? Now you are really in headache land. (An asside - what you would want to do is create a recursive descent parser. These parsers are used in compilers to interpret the source code to assembly or machine language. Recursive descent parsing is neat, neat stuff...)

With the Eval function, however, calculating a user's input takes one line of code! The Eval function, as its name suggests, evaluates an expression and returns the result. An expression is a regular VBScript expression, in string format. For example:

Dim strExpression, iResult
strExpression = "4 + 5 * 9"

iResult = Eval(strExpression)

You can also use the Eval function with the logical operators. For example:

If Eval("Variable1 > Variable2") then
  'Do something
End If

The Execute function is similar to the Eval function, except it executes a statement rather than evaluates it. For example, if you want to determine whether a variable is equivalent to some value, you could use the Eval function like so:

If Eval("SomeVariable = 4") then
  'Do something
End If

Be aware that letting your users evaluate or execute statements is a security risk.

If, however, you wanted to assign the value of 4 to the variable SomeVariable, you would need to use the Execute function like so:

'Assing the value of 4 to SomeVariable
Dim SomeVariable
Execute("SomeVariable = 4")

You can execute multiple statements with a single Execute function call; simply separate each statement with a colon (:), like:

Execute("SomeVariable = 4 : Response.Write SomeVariable")

Using the Execute statement you can accomplish some neat things, such as creating subroutines within subroutines. For some of these examples, be sure to check out the technical docs.

Note that the Eval and Execute functions are part of the VBScript 5.0 Scripting engine. If you have an outdated version of the scripting engine, you may have trouble with these two functions. To remedy this, simply download and install the latest version of the VBScript scripting engine on your Web server.

You may be asking yourself when anyone would use either the Eval function or the Execute function. Why not just hardcode in the statement into your ASP page? Usually you will use these two functions when you want to have a user's input directly effect the flow of your program, as in our mathematical example earlier in this article. If you have some good practical uses of these functions, be sure to share them! :)

Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article