Runtime Code Evaluation
The ability to evaluate code at runtime is a new feature in VBScript 5.0. Because this feature is new, many people aren't familiar with what runtime code evaluation is and how to use it.
What Is Runtime Code Evaluation?
To understand runtime code evaluation, you need to know the difference between how compiled-language scripts (e.g., C or C++ scripts) and interpreted-language scripts (e.g., VBScript or Perl scripts) execute. In a compiled-language script, a compiler reads the script's source code and translates it into low-level instructions that the hardware can understand, which is typically binary code. You then execute the binary code separately. If you use a compiled language, you must write a complete script before you can compile and execute it.
In interpreted-language scripts, a runtime module reads in the source code, translates the source code into binary code, and executes the instructions directly. This module reads, translates, and executes the source code line by line. Thus, you don't need to write a complete script before you execute it. . . .
<blockquote><vbs>
<br>Option Explicit<br>
sub DefinerFunc
<blockquote>Dim buf<br>
buf = "foobar"<br>
Execute "MsgBox buf"<br>
Execute "sub DefinedFunc: MsgBox buf: end sub"<br>
Call DefinedFunc</blockquote>
end sub<br>
DefinerFunc<br>
</vbs></blockquote>
This only outputs "foobar" once, not twice, because DefinedFunc can't see the buf variable defined in DefinerFunc. Too bad. So using Execute within a function to define another function only differs from ExecuteGlobal in that you get your own function naming scope, but the defined function sees only a global scope, even if the defining function is within a class. So you can't really define new member functions, either. Oh well, it's still good for some little stuff, and it's somewhat safer and simpler this way.<p>
Michael Balloni May 01, 2002