You are not Logged in
Would you like to Login or Register

Today is: Friday, 21 November, 2008
Check this months hot topics

Evaluating Expressions

You can evaluate sums in the fly using .net DataTables.

This is how you do it (C# 2005).

string sum = "(1+2)/4";
System.Data.DataTable evaluator = new System.Data.DataTable("temp");
double result;
 
result = (double)evaluator.Compute(sum, null);

This can be pretty damn useful. On a recent project we used tokenised strings which were dynamically replaced with values read from a serialized class.

Consider how powerful this can be. e.g. (VB.NET 2003)

Dim evaluator As New DataTable("temp")
Dim sum As String = "([apples]/[pears])*([oranges]*1.175)"
Dim result As Double
 
    sum = sum.Replace("[apples]", 23)
    sum = sum.Replace("[pears]", 1.21)
    sum = sum.Replace("[oranges]", 99)
 
result = evaluator.Compute(sum, Nothing)
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Tuesday, 13 June, 2006

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views