Store formula as a String in C#

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?

GsteingrAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
SStoryConnect With a Mentor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
dave4dlCommented:
The following site describes how to do what you want to do:
http://www.codeproject.com/csharp/evalcscode.asp
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...dave4dl...that's the same link that SStory gave over 1/2 hour ago.  =)
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
joechinaConnect With a Mentor Commented:
Check this
http://www.codeproject.com/cs/algorithms/matheval.asp

same idea as the link already posted, but easier to understand
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.