• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

How to embed and run a script

Hi all,

In my application, I need to allow my users to enter a mathematical script via the user interface. This script will include a combination of database columns, file fields, numerical operators, and numerical values, and will initially be held in the Text property of a TextBox.

I then need to embed that script into code, so it can be run multiple times within a batch process, but I have no idea how to do that. Can anyone suggest how I can make this work? Please note that I can't use SQL scripts as the input scripts aren't necessarily related to database processing.

Note that I will be restricting what a user can input by forcing them to enter the script using options on the screen i.e. through dropdown lists and buttons, so I know that what comes in will be syntactically correct.

I don't even know where to start on this one, so any help is greatly appreciated

Thanks
Gerry
0
gamesmeister
Asked:
gamesmeister
  • 2
1 Solution
 
OctabunCommented:
It is not very clear what you want to do.

From one side, script is a text and if not syntactically valid should be fixed according to reported errors. If correct, the script is run by a script engine. If you need that, select the scripting engine you like most, say, JScript, provide your custom top level objects to the script engine and let it run the script. This is the most generic approach, but your objects should be implemented as dispinterfaces, which may be tiresome.

From the other side, you speek about entering script by clicking controls. In this case you actually have some data structure that is filled while User clicks and displayed as text. Since it is a data structure, you may either write custom code to go through it and execute appropriate actions, or you may create code DOM structure and compile it into code.

So, my advise is to start with either making a choice or giving details of what you want to do.
0
 
gamesmeisterAuthor Commented:
Here's a simple example of what a user might enter.

First he selects a column from a database via a dropdown list. He clicks 'Add', which places the name of the column into a text box. He then clicks on a '+' button, which appends a '+' to the text in the textbox. He then enters a numerical value, and clicks 'Add' just as before. Then he clicks OK to store the script/formula he's created.

So the text of the textbox might look something like "Northwind.Product.Amount + 50". This can also be stored internally within three separate objects: (a) Northwind.Product.Amount  (b) +  (c) 50

What I then need to do is implement that formula as part of my application. As already mentioned, I can't just pass this in as a SQL statement as I won't always be dealing with database columns.

What do you mean by "create code DOM structure and compile it into code"?

Thanks
Gerry
0
 
OctabunCommented:
It looks like you are building something like a stack machine. What you have input is like

Push "Northwind.Product.Amount"   // parameter
GetNumberFromDB // command. what else can you do with the string above?
Push 50 // another parameter
Plus // command

You may store the above in an array of Object as

"Northwind.Product.Amount"
"cmd GetNumberFromDB"
50
"cmd Plus"

Next you may implemnt a stack of Object and go down the array as follows:

If it is not a string starting with "cmd", push to the stack, else call the appropriate procedure giving the stack as parameter. The procedure should pop its parameters from the stack and push its result. So, at the end the final result will be in the stack.

As I see it, there are 2 parts of it. The stack machine is based on the "inverse polish notation". You may search the Net on that or find a book looking for that in the index.

Working with objects is based on System.reflection and casts. Just in case, it is like

                  Object a;
                  Object b;
                  Object c;
                  a = "25";
                  if (a.GetType().Name == "String" )
                        c = Int32.Parse((String)a);
                     else
                        c = a;
                  b = 35;
                  c = (Int32)c+(Int32)b;
                  Console.WriteLine(c);
                  Console.ReadLine();

The code DOM allows you to create more advanced code fragments and compile it to code for speed. You may refer to "Code DOM quick reference" or "System.CodeDom" namespace reference in MSDN.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now