Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Store formula as a String in C#

Posted on 2007-04-05
9
380 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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