Home All Groups Group Topic Archive Search About

Simple Eval() (Ithink) question

Author
13 Sep 2006 10:02 PM
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



--
Posted via a free Usenet account from http://www.teranews.com

Author
13 Sep 2006 9:36 PM
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/
Show quoteHide quote
"Jeff" <n***@none.com> wrote in message
news:45086472$0$19715$88260bb3@free.teranews.com...
>
>
> 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
>
>
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>
Author
13 Sep 2006 9:46 PM
Tom Shelton
Jeff wrote:
Show quoteHide quote
> 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 :)

--
Tom Shelton
Author
13 Sep 2006 9:54 PM
Tom Shelton
Tom Shelton wrote:
Show quoteHide quote
> Jeff wrote:
> > 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 :)

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

--
Tom Shelton
Show quoteHide quote
>
> --
> Tom Shelton
Author
13 Sep 2006 9:50 PM
Herfried K. Wagner [MVP]
"Jeff" <n***@none.com> schrieb:
> 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>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
3 Mar 2007 10:24 PM
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.
Author
3 Mar 2007 11:54 PM
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

Show quoteHide quote
"stax" <nospam@nomail.no> wrote in message news:escsis$5hl$1@online.de...
> 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.
Author
4 Mar 2007 12:40 PM
stax
do you have sample code for this?

Jeff Jarrell wrote:
Show quoteHide quote
> 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
Author
4 Mar 2007 4:06 PM
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/


Show quoteHide quote
"stax" <nospam@nomail.no> wrote in message news:eseeog$640$1@online.de...
> do you have sample code for this?
>
> Jeff Jarrell wrote:
>> 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