Re: That Eval Question Again...




Jeff wrote:
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 :)

--
Tom Shelton

.



Relevant Pages

  • Re: Phils Test for the gospel standard of righteous judgement
    ... They are your instructions Jeff. ... 36 Did the word of God originate with you? ... let him acknowledge that what I am writing to you is the Lord's command. ... 15 And they said unto her, ...
    (uk.religion.christian)
  • Re: Command Prompt
    ... Keep having fun, Jeff. ... MS-MVP Windows Shell/User ... 'xxxx' is not recognized as an internal or external command, ... comfortable ground using the command prompt way because that's how i did ...
    (microsoft.public.windowsxp.basics)
  • Re: Suggestions for X10/Automation Widgets
    ... X-Lock" by Jeff Mazur that was along the lines of what The Alien is ... It listened to the powerline and, when it heard an alien X-10 ... command, jammed it by corrupting the signal. ... You need physical control of a device to enroll ...
    (comp.home.automation)
  • Re: create command buttons using vba
    ... "Jeff" wrote: ... "Doug Glancy" wrote: ... I need to space down each newly created command button ...
    (microsoft.public.excel.programming)
  • Re: 精力
    ... I just wanted to warn Konrad off constructing ... I think Jeff just expressed himself a little sloppily. ... > are both expressions of that rule, just a little bit of irregular ... Is this really referred to as the "masu stem?" ...
    (sci.lang.japan)