C# & Other .NET Compilers

 

Adding uCalc Fast Math Parser to your .NET application

 

The following explanation involves using uCalc as a .NET Reference component.  VB.NET also has the alternative option of including a header file instead.  See the VB.NET topic for that option.

 

In order to implement uCalc Fast Math Parser as a .NET Reference, follow these steps:

 

  1. Copy the uCalcNET.DLL, uCalc296.DLL, and ucFMP296.DLL files to your project's Bin (and/or Bin\Debug) directory.  Eventually, you may want to place them either in the Windows System directory or another appropriate directory in the path. 
  2. From the Visual Studio .NET menu, go to Project / Add Reference...
  3. Click on Browse.
  4. Find uCalcNET.DLL, and select it.
  5. Add a line such as the following to your code:

public uCalcNET.uCalcClass uc = new uCalcNET.uCalcClass();

 

Now you're all set to go.

 

Note:  Most of the .NET related files can be found in the ucFMP296\DotNet\ directory.  There you will find uCalcNET.dll, as well as the project-related source code files that were used to compile this wrapper DLL, using VB.NET.  The other required DLLs, uCalc296.dll and ucFMP296.dll, can be found in the ucFMP296\VB.NET directory.

 

The C# demo can be found in the ucFMP296\CS\ directory.

 

uCalc FMP is organized into a class for .NET.  Therefore, except from within callbacks, you will generally not need to specify a thread handle the way you might in one of the other supported compilers.  A uCalc object is defined with uCalcNET.uCalcClass.  Certain routines, such as ucEvaluate, ucReleaseItem, uCalc() and more are in a module, instead of the class, so ucEvaluate() would be called using uCalcNET.uCalcNET.ucEvaluate().

 

In .NET, arithmetic operators and trig functions, as well as ucParse and ucEvaluate operate using the Double precision type by default (whereas some other compilers use Extended precision instead).

 

 

Callbacks under managed code

 

There are fewer options for uCalc callbacks under .NET.  With the other compilers, you can use various non-generic data types, and you can choose to pass arguments by value or by reference.  Secondly, you can directly pass a function's address to uCalc with other compilers.  In .NET, you must use delegates for callbacks.  In .NET only the equivalent of a Native callback routine can be defined.  It is not necessary to use the Native: directive in your function and operator definitions, since it is already implied.  If you do use that word, it will simply work the same way as if you omit it.  Because you cannot permanently pin a uCalc variable to a variable in your host program (except if you use the unsafe directive in C#) callback routines may not be as speedy in .NET compilers as they are in other supported compilers.

 

Note: In version 2.96 ucParamHandle was added to other compilers.  It is synonymous with ucParamExpr, which is preserved for compatibility.  However, ucParamHandle is preferred.

 

 

Example 1:

 

C#

public uCalcNET.uCalcClass uc = new uCalcNET.uCalcClass();

...

textResult.Text = uc.ucEvalStr("1+2");

 

 

Visual Basic.NET

Dim uc As uCalcNET.uCalcClass

uc = New uCalcNET.uCalcClass

MsgBox(uc.ucEvalStr("1+2"))

 

 

Example 2:

 

In C# (unlike VB.NET), you have the option of declaring a section of code as unsafe, so that you can use pointers.  This allows you to attach a uCalc variable to the address of a variable in your host C# program.  For VB.NET, you would need to use ucSetVariableValue, which is commented out in the code below:

 

C#

unsafe private void buttonSpeedTest_Click(object sender, System.EventArgs e)

{

      int ExprHandle, xHandle;

      double x = 1, SumTotal = 0, SumMax, TimerStart, TimerEnd;

                 

      // Un-comment the following line if you prefer not to use the C# "unsafe" mode

      // xHandle = uc.ucDefineVariable("x As Double");

 

      // Remove the following line if you prefer not to use the C# "unsafe" mode

      xHandle = uc.ucDefineVariable("x As Double", (int)&x);

 

      ExprHandle = uc.ucParse(textSumExpression.Text);

      SumMax = System.Convert.ToDouble(textSumMax.Text);               

 

      TimerStart = Microsoft.VisualBasic.DateAndTime.Timer;

      while (x <= SumMax)

      {

            // Un-comment the following line if you prefer not to use the C# "unsafe" mode

            // uCalcNET.uCalcNET.ucSetVariableValue(xHandle, x);

            SumTotal = SumTotal + uCalcNET.uCalcNET.ucEvaluate(ExprHandle);

            x++;

      }

      TimerEnd = Microsoft.VisualBasic.DateAndTime.Timer - TimerStart;

    

      textSumResult.Text = System.Convert.ToString(SumTotal);

      labelElapsedTime.Text = "Elapsed Time:  " + System.Convert.ToString(TimerEnd);

  

      uCalcNET.uCalcNET.ucReleaseItem(ExprHandle);

      uCalcNET.uCalcNET.ucReleaseItem(xHandle);      

}

 

 

Avoiding verbosity

 

With the using keyword, you can replace uCalcNET.uCalcNET with something shorter, like uCalc.  To do this, place the following line in the using section of your C# code:

 

using uCalc = uCalcNET.uCalcNET;

 

You can then replace a line such as uCalcNET.uCalcNET.ucReleaseItem(ExprHandle);

with: uCalc.ucReleaseItem(ExprHandle);

 

 

Alternatively, you may enclose your code within this following construct:

 

namespace uCalcNET

{

   // Your code goes here.

}

 

Then you may use uCalcNET, instead of uCalcNET.uCalcNET.  And occurrences of uCalcNET.uCalcClass would now simply be uCalcClass.

 

 

Other C# Examples:

 

The following examples are for key features that may have some peculiarities in C#.  There are many more examples in Visual Basic that are general enough that a C# counter-part was not included.

 

Example 3:  Simple evaluation with ucEvalStr

Example 4:  Fast evaluation millions of times in a loop

Example 5:  Defining a centralized error handler

Example 6:  Raising an error with ucRaiseErrorMessage

Example 7:  A native function callback with two numeric arguments

Example 8:  A native function callback with any number of arguments

Example 9:  A native string callback function

Example 10:  Attached variables

 

See More Examples

 

 

New or enhanced in version 2.96