?
Solved

Store formula as a String in C#

Posted on 2007-04-05
9
Medium Priority
?
402 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
6 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 672 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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 664 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 664 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

850 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