Click here to Skip to main content
Click here to Skip to main content

String Parser and Evaluator

By , 7 Apr 2003
 

Sample Image - String_Evaluator.gif

Introduction

This formula evaluator can use different data types like int, double, string, DateTime or bool to calculate the value of a mathematical expression. You can use any of the operators, +, -, *, /, >, <, == etc., and specify parameters for them.

Background

.NET doesn't support types like Variant as in VB6 or Delphi. To avoid validating data type before doing any operation, I could create Variant type that can be used to save data of different types and overload some operators that are frequently used, like bitwise +, unary + -, *,/.

This code works in 2 steps: First, parse the expression and transform it to Backward Poland Notation. Now every order item can be either an operator or an operand. Second, calculate value of order using stack. Variables are saved in a public static field, that has the Hashtable type shown in the "string/value" view.

Using the code

To use this code, you must add a reference to the Evaluator.dll library.

Hashtable ht = new Hashtable();
ht.Add("A",12);
ht.Add("B",15);
Za.Evaluator.Evaluator.Variables = ht;
try
{
    textBox1.Text = Za.Evaluator.Evaluator.Evaluate("[A] + [B]/1.2").ToString();
}
catch(Za.Evaluator.CalcException ex)
{
    SomeCode xxxxxxxxx;
}
catch(Exception ex)
{
    SomeCode xxxxxxxxx;
}            

To realize addition functions, you need to edit EmbeddedFunction in Calculator.cs file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Zainaustria
Web Developer
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOperator Or (and,...)memberrichardlukes12 Nov '08 - 0:49 
GeneralUpdated Codememberzacksz26 Sep '08 - 11:03 
GeneralMuch easier to do this in .NET 2.0memberKristopher Cargile8 Apr '08 - 14:04 
Thanks to generics, doing this is much easier is .NET 2.0. See an example here: Parsing Delimited Strings Using Generics
GeneralA Bug needs attentionmembersakykhan30 May '07 - 14:04 
GeneralMinus/Substract Bug resolvedmemberAfersol003 Aug '06 - 9:47 
GeneralGood Work, but got a bugmemberMarais13 Mar '06 - 20:46 
Generalbugmemberwanwenguo20 Jan '05 - 20:42 
Generalbuggymemberdl4gbe21 Dec '04 - 16:12 
GeneralRe: buggymemberpatmarleon2 Jul '11 - 11:34 
GeneralI'd like to submit some improvementsmemberMike9513 Feb '04 - 5:44 
GeneralRe: I'd like to submit some improvementsmemberTony M John27 Feb '04 - 4:24 
GeneralRe: I'd like to submit some improvementsmemberemiliodabdoub25 Sep '07 - 21:12 
GeneralImprovementsmemberMatthew Dye28 Nov '03 - 6:51 
GeneralWhy not CodeDOMmemberJonathan de Halleux9 Oct '03 - 4:33 
GeneralBugmemberChibacity9 Oct '03 - 4:28 
GeneralRe: BugmemberTJ Manjaly1 Mar '04 - 7:30 
GeneralRe: BugmemberNaues16 Aug '04 - 4:43 
GeneralRe: BugmemberSerkan Kokturk20 Dec '06 - 3:16 
GeneralAnd &amp;&amp; Or operatorsmemberJ.M.Piulachs13 Sep '03 - 22:00 
QuestionUsage guidelines?sussArchieBunker22 May '03 - 10:57 
GeneralNicely DonememberBlake Coverett8 Apr '03 - 18:07 
GeneralRe: Nicely Donememberjdunlap1 Sep '03 - 12:50 
GeneralRe: Nicely Donememberemiliodabdoub25 Sep '07 - 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 8 Apr 2003
Article Copyright 2003 by Zainaustria
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid