|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
That Eval Question Again...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 wrote:
Show quoteHide quote > 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) Jeff - there are some very good suggestions in that list, and depending> > > 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 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 "Tom Shelton" <t**@mtogden.com> wrote in message news:1158189188.332993.146720@i42g2000cwa.googlegroups.com... Thanks, but again, I'm still in the early stages of learning. In other languages, this is> 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. 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 Jeff wrote:
> "Tom Shelton" <t**@mtogden.com> wrote in message news:1158189188.332993.146720@i42g2000cwa.googlegroups.com... While it's not in VB.NET - it is in JScript.NET. What I am showing you> > 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. 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 Well, there are limitations on the Express Editions. But, in this case> 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. > 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 My guess is no. It is simply a bat file that sets a bunch 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? > 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\jsc.exe /target:library evaluator.js -- Tom Shelton "Tom Shelton" <t**@mtogden.com> wrote in message news:1158194517.383610.44970@p79g2000cwp.googlegroups.com... 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> > 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. 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 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. Jeff wrote:
> Never mind that last question of mine Tom. I was able to find the answer on my own right after posting. I'm glad you got it working... And I hope it does what you need.> 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. -- Tom Shelton Jeff,
See this sample created from some basic code we got from Charles Law. http://www.vb-tips.com/dbPages.aspx?ID=34241f3e-16ff-4e87-86d1-2b03e1a439ae I hope this helps, Cor Show quoteHide quote "Jeff" <n***@none.com> schreef in bericht news:45087864$0$19605$88260bb3@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 > Cor Ligthert [MVP] wrote:
> Jeff, Cor,> > See this sample created from some basic code we got from Charles Law. > > http://www.vb-tips.com/dbPages.aspx?ID=34241f3e-16ff-4e87-86d1-2b03e1a439ae > > I hope this helps, > > 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 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. :-) CorShow quoteHide quote "Tom Shelton" <t**@mtogden.com> schreef in bericht news:1158211989.544683.287010@h48g2000cwc.googlegroups.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?ID=34241f3e-16ff-4e87-86d1-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 > Cor Ligthert [MVP] wrote:
Show quoteHide quote > Tom, Cor,> > 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 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
Simple Eval() (Ithink) question
gradient titlebar in vbdotnet Print the RichTextBox How to return value in Combobox Updating Files IIF referencing both conditional parts regardless of condition? ListBox, ComboBox Bug??? Compiled HTML helpfile does not work when application is installed .NET how to check binary compatability Naming convention of Variables |
|||||||||||||||||||||||