Solved

Store formula as a String in C#

Posted on 2007-04-05
9
371 Views
Last Modified: 2008-02-26
Hello,

Is it possable to store a formula as a string in c#?

F.ex. is it possable to store this as a string:
"(4 + num1) / 2"

Then later call :
int num1 = 4;

...and restore this formula somehow:
int result =  "(4 + num1) / 2"

Then call this string forumla some later time when one parameter, f.ex. num1 has changed?

0
Comment
Question by:Gsteingr
9 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18860924
No, not with native .Net...at least not without writing your own parser/function evaluator from scratch.

The Microsoft Script Control can do it though:
http://www.microsoft.com/mind/0799/script/script.asp
0
 
LVL 25

Accepted Solution

by:
SStory earned 168 total points
ID: 18861100
sounds like a great place for a function though

int F(int num1) {
      return (4 + num1) / 2
}

Or is this such that you don't want to write hard coded functions.

Does this help?
http://www.codeproject.com/csharp/evalcscode.asp
0
 
LVL 15

Expert Comment

by:dave4dl
ID: 18861306
The following site describes how to do what you want to do:
http://www.codeproject.com/csharp/evalcscode.asp
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18861406
Lol...dave4dl...that's the same link that SStory gave over 1/2 hour ago.  =)
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 166 total points
ID: 18861522
It's not quite what you asked for but just to give you an idea.

Instead of "Run" you can also do "one liners" with the "Eval" method...

Click on Project --> Add Reference --> COM Tab --> "Microsoft Script Control 1.0" --> "OK"

    public partial class Form10 : Form
    {

        private MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();

        private void Form10_Load(object sender, EventArgs e)
        {
            sc.Language = "VBScript";

            System.Text.StringBuilder code = new StringBuilder();
            code.AppendLine("function myFormula(num1)");
            code.AppendLine("    myFormula = (4 + num1) / 2");
            code.AppendLine("end function");

            sc.AddCode(code.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num1 = 8;
            object[] myParams = new object[] { num1 };
            int result = Convert.ToInt32(sc.Run("myFormula", ref myParams));
            MessageBox.Show(result.ToString()); // should be 6
        }

    }
0
 
LVL 13

Assisted Solution

by:joechina
joechina earned 166 total points
ID: 18861650
Check this
http://www.codeproject.com/cs/algorithms/matheval.asp

same idea as the link already posted, but easier to understand
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

15 Experts available now in Live!

Get 1:1 Help Now