Reputation: 1918
How can we save a formula into a table in my operation, I used to formula (Formula I can be dynamically created by the user) formula axample
Variabl1+Variable2*3.2+200
this formula isn't const.
thanks
Upvotes: 4
Views: 3194
Reputation: 11277
In C# you can use codeDOM services to dynamically compile C# code or you can dynamically build expression tree by parsing your formula string yourself. But probably both ways lacks performance because run-time compilation is needed
(huh, except maybe this one may perform good enough).
So because of that i would better suggest to embed some scripting engine to .NET framework - such as Lua or Javascript.
Still if you are writing asp.net
application - why you can't omit direct javascript code which computes formula result at the client side ? This probably would be the best solution because web applications and client side scripting are like husband and wife :-)
Upvotes: 1
Reputation: 4657
I think you have to save the exactly formula in database and when you want to use it, process the formula string using replace method. you should get Variabl1,Variable2 data and use this code:
string f="Variabl1+Variable2*3.2+200";
f= System.Text.RegularExpressions.Regex.Replace(f, "Variabl1", TxBxVar1.Text);
f= System.Text.RegularExpressions.Regex.Replace(f, "Variabl2", TxBxVar2.Text);
and then process f
to run the formula. for run you can use
System.Text.RegularExpressions.Regex.Split(f, "+");
and use this replace code for all math operators that you want.
note: this is my way. but maybe there is another ways in c# that I don't know.
Upvotes: 1