Simple Eval() (Ithink) question

  • Thread starter Joseph Bittman MVP MCSD
  • Start date
J

Joseph Bittman MVP MCSD

Sept. 13, 2006

I don't know about your code sample as I've never really seen the Eval
method used.

What most people do in that situation, is something like this:

If 5 > 4 then
label1.text = "It worked"
end if

or if you needed to make sure the first integer passed in by the user was
greater than a number you have in mind... then you could do something like
this: (where textbox1.text contains the user's number)

if textbox1.text > [yournumber] then
.....

Basically, the "if" statement automatically evaluates simple operations such
as > or < or =True... or "if [passedinboolean]=False AND Textbox1.text > 3
then" ... stuff like that.

Hope this helps!
--

Joseph Bittman
Microsoft Certified Solution Developer
Microsoft Most Valuable Professional -- DPM

Blog/Web Site: http://CactiDevelopers.ResDev.Net/
 
T

Tom Shelton

Jeff said:
I'm new to Visual Web 2005 using VB:

...trying to get something like the below to run.
Apparently, I need something other than the eval function, but what?

Thanks in advance

Jeff


Dim x As String
x = "5 > 4"

If Eval(x) Then
Label1.Text = "It Worked"
End If


1. Create a class in JScript -

// JScript Source Code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}

2. Compile it to a dll (evaluator.dll):

jsc /target:library evaluator.js

3. Reference the dll in your VB.NET project.

4. Write Code:

Dim ec As New EvalClass()
Dim expr As String = "(10 + 6) / 8"
Dim res As Double = CType(ec.Evaluate(expr), Double)

Console.WriteLine("{0} = {1}", expr, res)

The issues... Well, you can actually exectue arbitray bits of JScript
code through this so it might be wise to do a little input validation
(ex: use regex to make sure that the input is a valid mathmatical
expression) and error traping, since the eval function just returns
object - but it actually works pretty well :)
 
H

Herfried K. Wagner [MVP]

Jeff said:
I'm new to Visual Web 2005 using VB:

...trying to get something like the below to run.
Apparently, I need something other than the eval function, but what?

Dim x As String
x = "5 > 4"

If Eval(x) Then
Label1.Text = "It Worked"
End If

Hand-made:

MathLib
<URL:http://www.palmbytes.de/content/dotnet/mathlib.htm>

Dynamic compilation:

Build a Custom .NET "EVAL" Provider
<URL:http://www.eggheadcafe.com/articles/20030908.asp>

Runtime Compilation (A .NET eval statement)
<URL:http://www.codeproject.com/dotnet/evaluator.asp>

Evaluation of numeric expressions using J#:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.csharp/msg/9e95759b54a323a7>
 
T

Tom Shelton

Tom said:
1. Create a class in JScript -

// JScript Source Code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}

2. Compile it to a dll (evaluator.dll):

jsc /target:library evaluator.js

3. Reference the dll in your VB.NET project.

4. Write Code:

Dim ec As New EvalClass()
Dim expr As String = "(10 + 6) / 8"
Dim res As Double = CType(ec.Evaluate(expr), Double)

Console.WriteLine("{0} = {1}", expr, res)

The issues... Well, you can actually exectue arbitray bits of JScript
code through this so it might be wise to do a little input validation
(ex: use regex to make sure that the input is a valid mathmatical
expression) and error traping, since the eval function just returns
object - but it actually works pretty well :)

I also forgot to mention - you need to reference the
microsoft.jscript.dll in your vb.net project :)
 
J

Jeff

I'm new to Visual Web 2005 using VB:

....trying to get something like the below to run.
Apparently, I need something other than the eval function, but what?

Thanks in advance

Jeff


Dim x As String
x = "5 > 4"

If Eval(x) Then
Label1.Text = "It Worked"
End If
 
S

stax

using late binding:

'as first line put 'Option Strict Off' for late binding
Dim o As Object = CreateObject("MSScriptControl.ScriptControl")
o.Language = "VBScript"
MsgBox(o.Eval("1+1"))


another way using reflection (works also with C#):

Dim expression As String = "1+1"
Dim scriptControl As Object = CreateObject("MSScriptControl.ScriptControl")
scriptControl.GetType.InvokeMember("Language", BindingFlags.SetProperty, Nothing, scriptControl, New Object() {"VBScript"}, Nothing)
Dim value As Object = scriptControl.GetType.InvokeMember("Eval", BindingFlags.InvokeMethod, Nothing, scriptControl, New Object() {expression})
MsgBox(value.ToString)

Personally I rather skip the reflection hassle and use late binding, not for the entire project, but only for one file where all classes go that use reflection.
 
J

Jeff Jarrell

This isn't an answer to your question but when I think about the need to
EVAL() scripts I have been considering hosting a Powershell instance (in
your app, non visually). Obviously, this would only be effective where
scripts don't already exist and you aren't protecting an existing body of
work.

It does appear that Powershell is the next generation of scripting on the
MSFT platform.

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

jeff
 
J

Jeff Jarrell

I haven't actually coded to this. I was thinking about this as a way to
provide extensibility to the customer in a winforms app. Something like
providing a hook so that when a button a click there would be an opportunity
to provide a script there.

Aside from just looking and learning what a powershell script is, you can
start here for how to host powershell in your app.
http://msdn2.microsoft.com/en-us/library/ms714459.aspx

If you take a listen at some his old shows you can get a feel for what
powershell is all about.
http://www.hanselminutes.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top