Asked by:
how to calculate any kind of formula?
Question

User883541315 posted
Hi
I have the following problem: I have any formula: e.g. (10+5)^3255/5...
In asp classic I could calculate the formula by using: execute("result = " & Formula)
In asp.net I can't use 'execute' anymore. how can I calculate the formula? I had a look in the mathclass but can't find there anything.
best regards
Chris
Monday, August 12, 2019 5:49 AM
All replies

User719153870 posted
Hi bonsai8008,
In asp.net I can't use 'execute' anymore. how can I calculate the formula? I had a look in the mathclass but can't find there anything.As far as i know, in asp.net there's no such method to calculate a formula in a string format.
You may need to write your own calculate method. I believe you can find what you want here: MathParser.
Best Regard,
Yang Shen
Monday, August 12, 2019 7:06 AM 
User883541315 posted
Hi Yang Shen
Thanks for your the link.
What a pity. Something quite interesting and surly often used is in asp.net not usable anymore.
Will try to find out how to do it.
best regards
Chris
Monday, August 12, 2019 1:52 PM 
User475983607 posted
IIRC, Server.Execute() invokes an asp page. Eval() can invoke a string equation.
In ASP.NET is a different framework which uses objects but you can use code blocks to do something similar.
Results = <%= 1+2+3 %>
Or from the Code behind.
Results = <%= Results %>
public decimal Results { get; set; } protected void Page_Load(object sender, EventArgs e) { Results = 1 + 2 + 3; }
Monday, August 12, 2019 3:04 PM 
User883541315 posted
Thanks to all for the answers
I found out an easy way:
 you have to activate in the project the reference "Microsoft Script Control 1.0". this has the effect that in the directory "/bin" you will get the file "Interop.MSScriptControl.dll"
 then you can use the following function:
Formel = (5+3)*(8/2)+3*5
Resultat = BerechneFormel(Formel)
Public Function BerechneFormel(Formula As String) As Double
Dim SC As New MSScriptControl.ScriptControl
Dim Result As DoubleResponse.Write("<br>Z 1050) Formula: " + Formula)
SC.Language = "VBSCRIPT"Formula = Replace(Formula, ".", "") 'mögliche Tausendertrennung
Formula = Replace(Formula, ",", ".") 'Umwandlung in amerikanische Zahlen
Try
'SHOW THAT IT WAS VALID
Result = Convert.ToDouble(SC.Eval(Formula))
'MessageBox.Show("Math success, " & Formula & " equals " & Result.ToString)
Catch ex As Exception
'SHOW THAT IT WAS INVALID
'MessageBox.Show("Not a valid math formula for a double.") '==> you need a box called "MessageBox"
End Try
BerechneFormel = Result
'Response.Write("<br>Z 1063) Result: " + Result.ToString)
End Function
Thursday, August 15, 2019 9:46 AM