Click here to Skip to main content
13,348,874 members (79,458 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 7 Apr 2003

String Parser and Evaluator

, 7 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Formula parser for C#

Sample Image - String_Evaluator.gif


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.


.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();
Za.Evaluator.Evaluator.Variables = ht;
    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.


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

Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionLicense Pin
Member 1158981321-May-15 1:28
memberMember 1158981321-May-15 1:28 
GeneralOperator Or (and,...) Pin
richardlukes12-Nov-08 1:49
memberrichardlukes12-Nov-08 1:49 
GeneralUpdated Code Pin
zacksz26-Sep-08 12:03
memberzacksz26-Sep-08 12:03 
GeneralMuch easier to do this in .NET 2.0 Pin
Kristopher Cargile8-Apr-08 15:04
memberKristopher Cargile8-Apr-08 15:04 
GeneralA Bug needs attention Pin
sakykhan30-May-07 15:04
membersakykhan30-May-07 15:04 
GeneralMinus/Substract Bug resolved Pin
Afersol003-Aug-06 10:47
memberAfersol003-Aug-06 10:47 
GeneralGood Work, but got a bug Pin
Marais13-Mar-06 21:46
memberMarais13-Mar-06 21:46 
Generalbug Pin
wanwenguo20-Jan-05 21:42
memberwanwenguo20-Jan-05 21:42 
Generalbuggy Pin
dl4gbe21-Dec-04 17:12
memberdl4gbe21-Dec-04 17:12 
GeneralRe: buggy Pin
patmarleon2-Jul-11 12:34
memberpatmarleon2-Jul-11 12:34 
GeneralI'd like to submit some improvements Pin
Mike9513-Feb-04 6:44
memberMike9513-Feb-04 6:44 
It seems the author can't be reached. How I can I submit an update that contains:
- A bug fix in the - operator for doubles
- Many more Math functions
- && and || support

I would attach the changes here if I could
GeneralRe: I'd like to submit some improvements Pin
Tony M John27-Feb-04 5:24
memberTony M John27-Feb-04 5:24 
GeneralRe: I'd like to submit some improvements Pin
emiliodabdoub25-Sep-07 22:12
memberemiliodabdoub25-Sep-07 22:12 
GeneralImprovements Pin
Matthew Dye28-Nov-03 7:51
memberMatthew Dye28-Nov-03 7:51 
GeneralWhy not CodeDOM Pin
Jonathan de Halleux9-Oct-03 5:33
memberJonathan de Halleux9-Oct-03 5:33 
GeneralBug Pin
Chibacity9-Oct-03 5:28
memberChibacity9-Oct-03 5:28 
GeneralRe: Bug Pin
TJ Manjaly1-Mar-04 8:30
memberTJ Manjaly1-Mar-04 8:30 
GeneralRe: Bug Pin
Naues16-Aug-04 5:43
memberNaues16-Aug-04 5:43 
GeneralRe: Bug Pin
Serkan Kokturk20-Dec-06 4:16
memberSerkan Kokturk20-Dec-06 4:16 
GeneralAnd &amp;&amp; Or operators Pin
J.M.Piulachs13-Sep-03 23:00
memberJ.M.Piulachs13-Sep-03 23:00 
QuestionUsage guidelines? Pin
ArchieBunker22-May-03 11:57
sussArchieBunker22-May-03 11:57 
GeneralNicely Done Pin
Blake Coverett8-Apr-03 19:07
memberBlake Coverett8-Apr-03 19:07 
GeneralRe: Nicely Done Pin
jdunlap1-Sep-03 13:50
memberjdunlap1-Sep-03 13:50 
GeneralRe: Nicely Done Pin
emiliodabdoub25-Sep-07 22:13
memberemiliodabdoub25-Sep-07 22:13 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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