locked
Calculating a Formula from a String Using Runtime RRS feed

  • Question

  • I want runtime in gried row wise calculation and getting some null formula error.

    Anyone help me how to string equation Execute using scriptcontrol

    see below code

    public ScriptControl HCalc;

    string equation = "(((3000 * 2)/12)/27)";

    this.HCalc.ExecuteStatement("[R2Fml]=" + equation ); /* here error generated on execute time */

    RuntimeHelpers.GetObjectValue(this.HCalc.Eval("[R2Fml]"))

    Wednesday, April 11, 2018 1:52 PM

All replies

  • The ScriptControl class is System.Web.UI and the Forum for that type assistance is located in one of the Forums here ASP.Net Forums.

    I've never heard of this class till now.


    La vida loca

    Wednesday, April 11, 2018 3:23 PM
  • I see that Tom's (a former contributor and MVP) page is still active. 

    http://tom-shelton.net/index.php/2008/12/02/a-simple-method-for-evaluating-mathematical-expressions-at-runtime/


    Success
    Cor

    Wednesday, April 11, 2018 6:40 PM
  • Maybe you forgot to do ‘HCalc = new ScriptControl()’?

    See also a known simple method:

       string equation = "(((3000 * 2)/12)/27)";

     

       var dt = new DataTable();

       dt.Columns.Add( "e", typeof( double ), equation );

       dt.Rows.Add( 0 );

     

       double result = dt.Rows[0].Field<double>( 0 );

     

    Wednesday, April 11, 2018 9:17 PM
  • Maybe you forgot to do ‘HCalc = new ScriptControl()’?

    See also a known simple method:

       string equation = "(((3000 * 2)/12)/27)";

     

       var dt = new DataTable();

       dt.Columns.Add( "e", typeof( double ), equation );

       dt.Rows.Add( 0 );

     

       double result = dt.Rows[0].Field<double>( 0 );

     


    Telerik can't convert the 3rd line to VB.Net or I woulda posted the VB.Net version quoting your post.

    La vida loca

    Wednesday, April 11, 2018 9:22 PM
  • For VB.Net there are also string expression calculators like NCalc which according to the link is now at GitHub and isn't supported anymore but I suppose it works. And this The expression evaluator revisited (Eval function in 100% managed .NET) from Code Project. There's probably more out there too. I found FLEE (Fast Lightweight Expression Evaluator) at NuGet also.

    La vida loca

    Wednesday, April 11, 2018 9:35 PM
  • Hi Crazy_lmmortal,

    You code is c#, so your project is C#? If yes, you can post your question at c# forum to professional help at the next time.

    string strFormula = "(((3000 * 2)/12)/27)";
                //remember to replace variable before evaluation
                ScriptControl scriptCtrl= new ScriptControl();
                scriptCtrl.Language = "VBScript";
                object objResult = scriptCtrl.Eval(strFormula);

     If you project is vb.net, you can take a look the following code to resolve your issue.

    Dim strFormula As String = "(((3000 * 2)/12)/27)"
            'remember to replace variable before evaluation
            Dim scriptCtrl As New ScriptControl
            scriptCtrl.Language = "VBScript"
            Dim objResult As Object = scriptCtrl.Eval(strFormula)

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, April 12, 2018 5:34 AM