Skip to content

Getting started

amomra edited this page Apr 20, 2015 · 2 revisions

Getting started

The first thing to do is to download and install the muParserNET library into your project. This can be done through NuGet by right-clicking the project and selecting the option "Manage NuGet Packages..." in Visual Studion 2013.

Example code

Using muParserNET is pretty straightforward as you can see in the following example. The sample defines a parser variables ("a") and adds a user defined functions named "MyFunc". When using the parser make sure that you don't forget to catch possible exceptions of type ParserError.

using muParserNET;

public class Example
{

    // Function callback
    public static double MySqr(double val)
    {
        return val * val;
    }

    public static void Main(String[] args)
    {
        try
        {
            Parser p = new Parser();
            ParserVariable a = p.DefineVar("a", 1);
            p.DefineFun("MySqr", MySqr, true);
            p.Expr = "MySqr(a)*_pi+min(10,a)";

            for (int i = 0; i < 100; ++i)
            {
                a.Value = i;  // Change value of variable a
                Console.WriteLine(p.Eval());
            }
        }
        catch (ParserError e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Clone this wiki locally