T
Tom Shelton
Jeff said:I guess that I should have explained better (I'm stil new with this, so I may not be asking the questions in the best way)
Normally, one would use the simple code:
if x>y then
something
end if
...but I have a text string stored on a database column that represents something that I want to evaluate. In other words, instead
of knowing that I want to see if x>y in advance, I only know that I wish to determine whether the string stored in some table column
is true or not at run time. Apparently the eval command/function does this in other languages, but not in VB.
Perhaps some of the references that others have listed might work - it will take time to read and digest.
If anyone can explain how to do this in very simple language that a beginner can understand, I would appreciate it.
Jeff
Jeff - there are some very good suggestions in that list, and depending
on the complexity of the expressions, then some of them maybe more
suitable then others (see Herfried's list). Here is the one I was
suggesting. This works really well if your expressions are simple
comparisons/mathmatical expressions - but it does require a little bit
of error trapping and input validation, because it can allow arbitrary
execution of jscript code, so it can potentially do dangerous things if
you aren't carefull with your input.
To use this suggestion:
1. Create a file called evaluator.js and add the following code:
// JScript Source Code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}
2. start the vs.net command line and get to the directory where you
saved this file and type in the following command:
jsc /target:library evaluator.js
this will create a dll in that directory called evaluator.dll
3. Create a simple console application in VS.NET and add a reference
to the compiled evaluator.dll and microsoft.jscript.dll.
4. Here is the code:
Option Strict On
Option Explicit On
Imports System
Module Module1
Sub Main()
Dim s As String = "5>4"
Dim e As New EvalClass()
If DirectCast(e.Evaluate(s), Boolean) Then
Console.ForegroundColor = ConsoleColor.DarkGreen
Console.WriteLine("It Worked!")
Else
Console.ForegroundColor = ConsoleColor.DarkRed
Console.WriteLine("It failed!")
End If
End Sub
End Module
You should get a green message that says "It Worked!". Like I said, in
the production application, you'll want to make sure that the input to
EvalClass.Evaluate is safe. If all your doing is simple
comparisons/mathmatical operations, then this is relatively easy to
accomplish with a couple lines of RegEx.
Is that simple enough? Given these steps you can at least experiment
with it