Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C#

A Calculation Engine for .NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (183 votes)
1 Sep 2013Public Domain15 min read 669.6K   11.4K   421  
A calculation engine that is small, fast, and extensible.
Imports System.Collections
Imports System.Reflection
Imports System.Globalization

Public Class Form1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' create calc engine
        Dim ce As New CalcEngine.CalcEngine()

        ' register custom function
        ce.RegisterFunction("Add", 2, AddressOf Add)

        ' test
        Console.WriteLine(ce.Evaluate("Add(420, 123+123)"))

    End Sub

    Private Shared Function Add(ByVal p As List(Of CalcEngine.Expression)) As Object
        Return CDbl(p(0)) + CDbl(p(1))
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles _btnEval.Click
        Dim ce As New CalcEngine.CalcEngine()
        _lblResult.Text = ce.Evaluate(_txtExpression.Text).ToString()
    End Sub
End Class





By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer
Brazil Brazil
Software Architect/Developer with several years experience creating and delivering software.

Full-stack Web development (including React, Firebase, TypeScript, HTML, CSS), Entity Framework, C#, MS SQL Server.

Passionate about new technologies and always keen to learn new things as well as improve on existing skills.

Comments and Discussions