Custom Search
Logiclabz
  • Home
  • C#
  • Evaluate function in C# .Net as Eval() function in Javascript

Evaluate function in C# .Net as Eval() function in Javascript

  

Evaluate function in C# .Net Alternate methods to evaluate a string and retrieve a result as Eval() function in Javascript.

Eval() function in Visual basic or javascript evaluates a string and executes it as if it was script code. Thereby we can pass a string containing a valid expression and retrieve a result on execution.

Unfortunately, neither C# nor Visual Basic .NET has the same functionality. I think this is a big difference between a compiled language and a interpreted one.

I found two simple methods in .Net to implement Evaluate function which would evaluates a string and gives the result.

Method 1 :

	public static double Evaluate(string expression)
        {
            return (double)new System.Xml.XPath.XPathDocument
            (new System.IO.StringReader("<r/>")).CreateNavigator().Evaluate
            (string.Format("number({0})", new
            System.Text.RegularExpressions.Regex(@"([\+\-\*])").Replace(expression, " ${1} ")
.Replace("/", " div ").Replace("%", " mod ")));
        }

In Method 1 System.Text.RegularExpressions.Regex helps in giving space between operator and also to replace "/" operator to div and "%" operator to mod. XPathNavigator.Evaluate method evaluates the string representing an XPath expression and returns the typed result.

Method 2 :

	public static double Evaluate(string expression)
        {
            System.Data.DataTable table = new System.Data.DataTable();
            table.Columns.Add("expression", string.Empty.GetType(), expression);
            System.Data.DataRow row = table.NewRow();
            table.Rows.Add(row);
            return double.Parse((string)row["expression"]);
        }

In Method 2 DataColumn.Expression in System.Data.DataTable is used to create a default row with required expression. Thereby DataTable calculate the values in a column and gives the evaluated result. Resultant datatype for Evaluate function in method 2 can changed to evaluate boolean expression and to get boolean results.

System.Console.WriteLine(Evaluate("2 * 4"));

Result would be "8".



  


Leave a reply


Comments

  • Ruben says:
    Jul 24, 09

    Great post. It works perfect with boolean to.

  • Sebahattin says:
    Dec 02, 09

    Çok tesekkürler..! Tankyou.

  • s1tony says:
    Aug 12, 10

    I am using method 2 and it works great. Good idea and thank you for posting.

  • Elliot says:
    Sep 13, 10

    I copied Method 2 in to my C# 4.0 program and got the following error message: System.Data.EvaluateException was unhandled Message=Cannot find column [x]. Source=System.Data StackTrace: at System.Data.NameNode.Bind(DataTable table, List`1 list) at System.Data.BinaryNode.Bind(DataTable table, List`1 list) at System.Data.DataExpression.Bind(DataTable table) at System.Data.DataColumn.SetTable(DataTable table) at System.Data.DataColumnCollection.BaseAdd(DataColumn column) at System.Data.DataColumnCollection.AddAt(Int32 index, DataColumn column) at System.Data.DataColumnCollection.Add(String columnName, Type type, String expression) at UseBasicFormulaInXls.Program.Evaluate(String expression) in C:\Users\evermes\Documents\c#Programs\UseBasicFormulaInXls\UseBasicFormulaInXls\Program.cs:line 122 at UseBasicFormulaInXls.Program.Main(String[] args) in C:\Users\evermes\Documents\c#Programs\UseBasicFormulaInXls\UseBasicFormulaInXls\Program.cs:line 47 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

  • Scott Smith says:
    Nov 03, 10

    I have looked far and wide for a way to evaluate Boolean expressions dynamically in C#. Many say it can't be done; others propose code-heavy tricks. Your solution is a marvel -- lightweight, easy to set up and use, works great.

  • batman says:
    Dec 28, 10

    you are so brilliant! can I kiss you?

  • Luis Roquette Valdez says:
    Jun 15, 11

    How's this for a generic Eval? public static TReturn Evaluate&lt;TReturn&gt;(string expression) { using (var evaluatingTable = new DataTable()) { evaluatingTable.Columns.Add("formula", typeof(TReturn), expression); evaluatingTable.Rows.Add(evaluatingTable.NewRow()); return (TReturn)evaluatingTable.Rows[0]["formula"]; } }

  • ateek says:
    Aug 09, 11

    Excellent Post.



Do you like this post?