C# Example
Table of ContentsProducts

Below is example C# code that demonstrates user defined methods, use of variables and exception handling.

Step by step description:

1. Create an instance of MathParser
2. Create a User Defined variable “J” with an initial value of 1.0
3. Assign value 5 into predefined variable X.
4 . Assign value 3 into variable Y.
5. Define user functions: It is possible to use .NET delegates to create functions that take 1 or 2 parameters and return a double. This is a common case in math expressions. But the generic way of defining a custom function with bcParser.NET is to implement the IFunction interface. You can have functions that take different numbers of parameters (or no parameters at all) and return different types of results (System.IConvertible which all types like double, int, string etc implement).
6. Turn optimization on. In this case this doesn’t have any effect since we do not repeatedly get the value after changing variable values. This is typically useful for applications where expression remains the same but variable values change. (charting applications where you need to compute value of a function at various x,y points)
7. Set an expression to be evaluated.
8. Loop through variables and display their values.
9 . Get the result value and write it to the console.
10. Handle the exception by displaying invalid portion of the expression if needed.

Output would look like:

using System;
  using
Bestcode.MathParser;

  namespace
TestParser {
    class
Class1 {
      []

      static void
Main ( string []args ){
        MathParser parser =
new MathParser ();
        try
{
          parser.Expression =
"X" ;
          parser.Evaluate () ;
          parser.CreateVar (
"J" , 1 , null ) ;
          parser.X =
5 ;
          parser.SetVariable (
"Y" , 3 , null ) ;
          parser.CreateOneParamFunc (
"ADD500" , new OneParamFunc (MyADD500Func)) ;
          parser.CreateTwoParamFunc (
"MULT2" , new TwoParamFunc (MyMultiplyFunc2)) ;
          parser.CreateFunc (
"MULTN" , new MyMultiplyN ()) ;
          parser.OptimizationOn =
true ;
          parser.Expression =
"ADD500(X+Y+MULTN(2,X,Y)+MULT2(2,3))" ;
          String vars[] = parser.GetVariables () ;

          foreach
(String var in vars) {
            System.Console .WriteLine (
"Variable " + var + " = " + parser.GetVariable (var) ) ;
          }
          System.Console .WriteLine (parser.Expression +
" = " + parser.Value ) ;
        }

        catch
(ParserException pe) {
          System.Console .WriteLine (pe.Message ) ;
          System.Console .WriteLine (pe.GetInvalidPortionOfExpression () ) ;
        }

        catch
(Exception ex){
          System.Console .WriteLine (ex.Message ) ;
          System.Console .WriteLine (ex.StackTrace ) ;
        }
        System.Console .ReadLine () ;
      }

      static public double
MyADD500Func (IParameter x ){
        return
x.GetValueAsDouble () + 500 ;
      }

      static public double
MyMultiplyFunc2 (IParameter x , IParameter y ){
        return
x.GetValueAsDouble () * y.GetValueAsDouble () ;
      }

      class
MyMultiplyN :IFunction {
        public
IConvertible Run (IParameter []p ){
          if
(p.Length < 1 ){
            throw new
Exception ( "MyMultiplyN requires at least 1 parameter." );
          }

          double
total = 1.0 ;
          foreach
(IParameter param in p) {
            total *= param.GetValueAsDouble () ;
          }

          return
total;
        }

        public int
GetNumberOfParams (){
          return
- 1 ;
        }

      }


    }

  }

webmaster@bestcode.com