0

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

1
  • The variable x in your code is in a different environment to the one in SC. Have you tried replacing the string "x" in the string_from_db with the string "10" and then evaluating it? Commented Oct 7, 2018 at 17:16

1 Answer 1

2

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:

SC.Language = "VBSCRIPT"
sc.AddCode "Private Const x = 10"

Dim Result As Double = Convert.ToDouble(SC.Eval(Formula))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.