473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

That Eval Question Again...


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


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

Sep 13 '06 #1
10 1365

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

Sep 13 '06 #2

"Tom Shelton" <to*@mtogden.comwrote in message news:11**********************@i42g2000cwa.googlegr oups.com...
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.
Thanks, but again, I'm still in the early stages of learning. In other languages, this is
a simple built-in function. What is particularly frustrating is that there are so many small
variations of languages and scripts and similar, that if I find a possible answer on the
internet, I spend minutes if not hours eventually determining that it isn't applicable to
my Visual Web 2005 express edition.

For example, I've now searched just about everywhere for the simple piece of
information about how to open the vs.net command line that you mentioned. I can find no
..exe anywhere under various names mentioned through a google search, nor any type of
command line processor within the Visual Web GUI like in FoxPro.

So, can someone explain simply how to "open the vs.net command line"
Is this something not found in the Express version?

I understand the rest.

Thanks

Jeff

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

Sep 14 '06 #3

Jeff wrote:
"Tom Shelton" <to*@mtogden.comwrote in message news:11**********************@i42g2000cwa.googlegr oups.com...
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.

Thanks, but again, I'm still in the early stages of learning. In other languages, this is
a simple built-in function.
While it's not in VB.NET - it is in JScript.NET. What I am showing you
is a technique for exposing that to your VB.NET application. This is
one of the great things about .NET. You are able to use classes and
object written in other languages.
What is particularly frustrating is that there are so many small
variations of languages and scripts and similar, that if I find a possible answer on the
internet, I spend minutes if not hours eventually determining that it isn't applicable to
my Visual Web 2005 express edition.
Well, there are limitations on the Express Editions. But, in this case
it should be ok, because you have to compile the dll on the command
line :)
For example, I've now searched just about everywhere for the simple piece of
information about how to open the vs.net command line that you mentioned. I can find no
.exe anywhere under various names mentioned through a google search, nor any type of
command line processor within the Visual Web GUI like in FoxPro.

So, can someone explain simply how to "open the vs.net command line"
Is this something not found in the Express version?
My guess is no. It is simply a bat file that sets a bunch of
environment variables and then runs the command line. Basically,
providing a .NET command line environment. I'm pretty sure there is no
such thing with the express editions. So, in your case you will either
need to add the framework directory to your path or type in the
complete path to the jsc.exe. Example:

C:\C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\j sc.exe
/target:library evaluator.js

--
Tom Shelton

Sep 14 '06 #4

"Tom Shelton" <to*@mtogden.comwrote in message news:11*********************@p79g2000cwp.googlegro ups.com...
>
While it's not in VB.NET - it is in JScript.NET. What I am showing you
is a technique for exposing that to your VB.NET application. This is
one of the great things about .NET. You are able to use classes and
object written in other languages.

Thanks for putting up with me. I've almost got it and it is one of the last snags I need to overcome to finish
a web application that I've been working months on. Please hang in here with me and point me in the direction of the one thing that
I'm still missing. To re-cap:

I have an "evaluator.js" file that looks like this:
// JScript File
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}

I was able to create the evaluator.dll file. That .dll currently is in the ...\Framework\v2.0.50727\ folder

I created a "TestEvaluator.apsx" file on my server where I have the app. Here is the code (a slight variation of what you provided,
as I'm not sure I know what exactly you mean by a "console application." (isn't that for the regular vb and not for vweb?)

Here is the code in my TestEvaluator.aspx.vb
Option Strict On
Option Explicit On
Imports System
Partial Class TestEvaluator
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = "5>4"
Dim g As New EvalClass()

If DirectCast(g.evaluate(s), Boolean) Then
Label1.Text = "It Worked"
Else : Label1.Text = "It Failed"
End If
End Sub
End Class

I'm being told that, "Type EvalClass is not defined"
I'm sure that I need only a line or two more to make this work.
Can you please tell me what is missing?

Thanks very much - I'm almost there and can get all else working on my own if I can only get this example working (that simply
changes the label when the button is clicked)

Jeff





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

Sep 14 '06 #5


Never mind that last question of mine Tom. I was able to find the answer on my own right after posting.
Here it is for anyone attempting the same.

Thanks very much for the help and the step-by-step instructions for a newbee...
I've already started messing with the code and it appears to do what I need.

Jeff
To add an assembly DLL to your Web site project
1.. In Solution Explorer, select your Web site.

2.. On the Website menu, choose Add Reference. Alternatively, you can right-click the name of your Web site and then select Add
Reference.

The Add Reference dialog box is displayed.

3.. Select the Browse tab.

4.. Navigate to the folder containing the assembly you want to reference, select the assembly, and then click OK.

Adding a reference in this way ensures that all file dependencies (debug files, XML document files, and so on) are copied.

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

Sep 14 '06 #6

Jeff wrote:
Never mind that last question of mine Tom. I was able to find the answer on my own right after posting.
Here it is for anyone attempting the same.

Thanks very much for the help and the step-by-step instructions for a newbee...
I've already started messing with the code and it appears to do what I need.
I'm glad you got it working... And I hope it does what you need.

--
Tom Shelton

Sep 14 '06 #7
Jeff,

See this sample created from some basic code we got from Charles Law.

http://www.vb-tips.com/dbPages.aspx?...1-2b03e1a439ae

I hope this helps,

Cor
"Jeff" <no**@none.comschreef in bericht
news:45***********************@free.teranews.com.. .
>
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


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

Sep 14 '06 #8

Cor Ligthert [MVP] wrote:
Jeff,

See this sample created from some basic code we got from Charles Law.

http://www.vb-tips.com/dbPages.aspx?...1-2b03e1a439ae

I hope this helps,

Cor
Cor,

I personally would avoid using the MS Script control for several
reasons:

1. It requires COM interop, so it can severaly impact performance if
it is used often. I usually try to look for solutions that stay inside
of the framework if at all possible.

2. It doesn't always work because windows scripting host is turned off
for security reasons.

3. If I remember correctly (and I may not :), this particular control
has been broken in the past due to interface changes

I'm not saying that this isn't a valid solution - but wrapping the
JScript.NET eval function is fairly trivial, and it avoids the over
head of COM interop. Still, as always, YMMV.

--
Tom Shelton

Sep 14 '06 #9
Tom,

I tried it and it works great. Charles made it first in a kind of fragmental
way and because there are so many questions about this I thought let me put
it on our website. Than it is of course set on that to show.

You should now that I am the one who is probably the most writing against
any not needed Com interop use, Api use, reflection use, and with that eval
use, however I see so many people making propaganda for those, you should
forgive me this sine because this is so easy to use.

I see the one I have showed only to be used in a kind of demo situations,
not in real business.

:-)

Cor

"Tom Shelton" <to*@mtogden.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.com...
>
Cor Ligthert [MVP] wrote:
>Jeff,

See this sample created from some basic code we got from Charles Law.

http://www.vb-tips.com/dbPages.aspx?...1-2b03e1a439ae

I hope this helps,

Cor

Cor,

I personally would avoid using the MS Script control for several
reasons:

1. It requires COM interop, so it can severaly impact performance if
it is used often. I usually try to look for solutions that stay inside
of the framework if at all possible.

2. It doesn't always work because windows scripting host is turned off
for security reasons.

3. If I remember correctly (and I may not :), this particular control
has been broken in the past due to interface changes

I'm not saying that this isn't a valid solution - but wrapping the
JScript.NET eval function is fairly trivial, and it avoids the over
head of COM interop. Still, as always, YMMV.

--
Tom Shelton

Sep 14 '06 #10

Cor Ligthert [MVP] wrote:
Tom,

I tried it and it works great. Charles made it first in a kind of fragmental
way and because there are so many questions about this I thought let me put
it on our website. Than it is of course set on that to show.

You should now that I am the one who is probably the most writing against
any not needed Com interop use, Api use, reflection use, and with that eval
use, however I see so many people making propaganda for those, you should
forgive me this sine because this is so easy to use.

I see the one I have showed only to be used in a kind of demo situations,
not in real business.

:-)

Cor
Cor,

I hope you don't think I was criticizing you or your answer. Wrapping
the MS Script Control is a perfectly valid solution. I was just
pointing out some of it's short commings, especially when you can
accomplish the task without going outside of the framework.

--
Tom Shelton

Sep 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Julian Hernandez Gomez | last post by:
Hi ! This is maybe a silly question, but... is there a "easy way" to make eval() convert all floating numbers to Decimal objects and return a Decimal? for example: ...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
4
by: | last post by:
Hi all, Does C# have an Eval function like JScript does? I have a need to evaluate some code on the fly... TIA!
4
by: bob garbados | last post by:
I need to create a page that displays all of the products from a table and allows for add to cart functionality. My thoughts were to display all of the products in table rows using a repeater. ...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
0
by: Geert Jansen | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm experiencing a weird problem with eval(), providing it a copy of the globals() dictionary. The following code works:
5
by: R | last post by:
Hi All, I'm using eval and arrays in foreach and have trouble with adding elements to them - I'm talking about the '' operator. My code is: // creates arrays with the names of columns in...
3
by: Andrew | last post by:
I need to call a function inside another function, but I don't know the function name. I solved in this way: function x (newFunc) { eval(newFunc+ "()"); } Again this work for Firefox and...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.