I like to populate my database with different formulas.
When I have a formula in the database like "(2+4)*5"
I get the result 40
:
update DB.dbo.INFOGROUP SET formula = '(2+4)*5' where INFOGROUP = 'S42'
When I have a formula in the database like "(x+4)*5"
I get the result 20
. The x
value is ignored even when I set the value of x
to 10
:
update DB.dbo.INFOGROUP SET formula = '(x+4)*5' where INFOGROUP = 'S42'
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
Dim SC As New MSScriptControl.ScriptControl
Dim x As Double = 10
SQL.AddParam("@infogroup", "S42")
SQL.ExecQuery($"SELECT formula FROM DB.dbo.INFOGROEP where infogroep=@infogroep")
Dim Formula As String = <string_from_db>
'SET LANGUAGE TO VBSCRIPT
SC.Language = "VBSCRIPT"
'ATTEMPT MATH
Try
Dim Result As Double = Convert.ToDouble(SC.Eval(Formula))
'SHOW THAT IT WAS VALID
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.")
End Try
End Sub
Is this possible at all or do I need a third party script for that?
BTW: in Visual Foxpro there was a function like EVAL(db_string)
.
The script engine has no knowledge about variables in your methods. If you want to evaluate a formula that contains a variable, you need to tell it what value that variable has: