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"

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

None

0 Points

8 Posts

## how to calculate any kind of formula?

Aug 12, 2019 05:49 AM|bonsai8008|LINK

Hi

I have the following problem: I have any formula: e.g. (10+5)^3-255/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 math-class but can't find there anything.

best regards

Chris

Contributor

3140 Points

983 Posts

## Re: how to calculate any kind of formula?

Aug 12, 2019 07:06 AM|Yang Shen|LINK

Hi bonsai8008,

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

None

0 Points

8 Posts

## Re: how to calculate any kind of formula?

Aug 12, 2019 01:52 PM|bonsai8008|LINK

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

All-Star

53041 Points

23612 Posts

## Re: how to calculate any kind of formula?

Aug 12, 2019 03:04 PM|mgebhard|LINK

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.

Or from the Code behind.

`Results = <%= Results %>`

None

0 Points

8 Posts

## Re: how to calculate any kind of formula?

Aug 15, 2019 09:46 AM|bonsai8008|LINK

Thanks to all for the answers

I found out an easy way:

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 Double

Response.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