How to embed and run a script

Posted on 2005-04-29
Last Modified: 2010-04-16
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

Question by:gamesmeister
    LVL 3

    Expert Comment

    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.
    LVL 4

    Author Comment

    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"?

    LVL 3

    Accepted Solution

    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

    "cmd GetNumberFromDB"
    "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);
                            c = a;
                      b = 35;
                      c = (Int32)c+(Int32)b;

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now