Re: That Eval Question Again...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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: Secret Christian donors bankroll Tories
    ... I am not arguing this one with you Jeff as you know as your position ... justice" and strive along with unbelievers for a "better world", ... and dig a hole when they wished to relieve themselves he didn't mean ... There is no compromise of the command of God in loving our neighbour ...
    (uk.religion.christian)
  • Re: Bibles: KJV & CSL
    ... The problem Jeff is that firstly you major on the minors and secondly you ... You do not obey the command to gather together, ... Some might see that as hypocrisy. ... for what you would not do in the congregation you should not do here. ...
    (uk.religion.christian)
  • Re: Waffen ss
    ... yet most Christians don't accept the way ... Jeff is not a Biblical scholar. ... So all such "Irrelevant" verses which include Christ's own command for women, pass well over your head as can be seen in your posts, time after time throughout the year. ... 35 For I have come to set a man against his father, and a daughter against her mother, and a daughter-in-law against her mother-in-law. ...
    (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)