Solved

Store formula as a String in C#

Posted on 2007-04-05
9
381 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 86

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

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

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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