Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
A reader asked me recently whether there was a way to check a chunk of JScript or VBScript for syntax errors without actually running the code. I'm sure that there are many third-party tools which you could find that do this. If you have your own script host, you can do it yourself quite easily.
The trick is to initialize the engine by setting the script site, but do not do anything that would move the engine from initialized into started state. (Note that attempting to evaluate an expression moves the engine to started state.)
If the engine is initialzed but not started then calling ParseScriptText will check the text passed in for syntax errors but will not run the script. Rather, if the script compiles successfully, it is simply marked as "needs to run when the engine is started".
Unfortunately the script engines do not have an error-recovering parser, so they will detect only the first syntax error and then bail out. Still, better than nothing.
If the code contains functions and subroutines only (no event handlers),
the code will validate without running.
What is the function name in VB.NET that is equal to eval function in VBScript?
I am working with VB.NET, I have many mathematical equations in my windows application, which differ according to many options I selected from previous form. So I m using thesse equation as strings in database and according to the selected options from the previous form it drag the its equation, which I concatinate to a variable (number) according to the selected age. So this equation doesn't work because it defined as nvchar in the databse and the variable is number, I tried the function eval() which I use in asp to solve this but it didn't work. I want to know if there is a function in VB.NET can solve this problem or what can I do to solve it.
There is an easy way to do syntax testing when hosting the script control: At runtime, just add the line "err.raise 9999" as very first line to the script and enable error handling in your code that hosts the control. After calling .AddCode(...), test the error object of the script control: If it contains err.number=9999, then there is no syntax error. No code will be run, except the err.raise statement.