# Andrej Tozon's blog

## In the Attic

NAVIGATION - SEARCH

## Adventures in Calculus, pt. 1

Evaluating simple mathematical expressions in .NET may not appear as an easy task. I mean, how would you code something like Eval("1+1")? While there are more complex solutions to such problem, like using JScript's Eval.JScriptEvaluate(), you can also hook up the DataTable class and its Expression columns to perform some simple calculations.

For this post's example, I created a simple Calculator C# project, which allows a user to enter a simple math expression and watch the result being calculated while typing. There were very few lines of code required to code this sample, since most of the hard work is done by DataTable and Data Binding functionality. The expression can be entered by typing directly into the box or by pressing desired buttons. Each button's Click event is wired to the same button_Click handler, which additionally reduces the code line count:

private void button_Pressed(object sender, EventArgs e)
{
display.AppendText(((Button)sender).Text);
}

The core of this project is the Calculator class, which holds the DataTable and actually performs the calculations. A DataTable with single column and single row, holding the default value (0), is generated in its constructor:

public Calculator()
{
DataTable table = new DataTable();
column = table.Columns.Add("Result", typeof(decimal));
row = table.Rows.Add(new object[] { 0m });
}

Calculator class exposes two properties; one for expression, and one for the result. Expression property controls the Expression property of DataTable's only column, and Result only returns the value, being calculated by the DataTable:

[Bindable(true, BindingDirection.OneWay)]
public decimal Result
{
get { return row.IsNull(0) ? 0 : (decimal)row; }
}

[Bindable(true)]
public string Expression
{
get { return column.Expression; }
set { column.Expression = value; }
}

There you go, this is your math calculator engine. And when is the actual calculation performed, you ask? Well, that's the magic DataTable provides for you. By setting the DataColumn's Expression property to a (valid) expression, the value in this column will be automatically calculated by the Framework.

To finalize this example, let's connect our input and result fields with the Calculator class. Two lines of Data Binding code are required:

display.DataBindings.Add("Text", calculator, "Expression", true, DataSourceUpdateMode.OnPropertyChanged);
result.DataBindings.Add("Text", calculator, "Result", true, DataSourceUpdateMode.Never);

display is a TextBox, used by user to enter the expression, which is passed to the Calculator class through Data Binding. Similarly, result TextBox will display whatever comes from the same class.

Well, that was easy, and you've now got yourself a fully functional calculator. In further posts, we'll explore additional calculation options DataTable provides.

Download this sample

[Update: you can try the compiled Calculator sample without downloading and compiling the code. Just run it through this ClickOnce page]