C#
& Other .NET Compilers
Adding uCalc Fast Math Parser
to your .NET application
Previous versions had a separate way of implementing uCalc with .NET. As of version 3.0 you simply add the include file to your project, and make sure the DLLs are in the path (no need to use Add Reference), and you’re ready to go. In C#, instead things like uCalcNET.uCalcNET.ucReleaseItem it’s now uCalc.ucReleaseItem. There is no longer an additional uCalcNET.dll file.
Note: Version 3 was designed to work with C# 2010 and C# 2012. For earlier versions of C#, you may either revise the uCalc header, or else use uCalc FMP 2.96. Also, C# examples in this documentation were originally intended for earlier versions of C#. The examples were manually revised but not tested. Sample code that was tested with C# 2010 and C# 2012 can be found in the demo program that comes with the uCalc FMP download.
Example 1:
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 = uCalc.ucDefineVariable("x
As Double"); //
Remove the following line if you prefer not to use the C# "unsafe"
mode xHandle = uCalc.ucDefineVariable("x
As Double", (int)&x); ExprHandle = uCalc.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 //
uCalc.ucSetVariableValue(xHandle,
x); SumTotal = SumTotal + uCalc.ucEvaluate(ExprHandle); x++; } TimerEnd = Microsoft.VisualBasic.DateAndTime.Timer
- TimerStart; textSumResult.Text = System.Convert.ToString(SumTotal); labelElapsedTime.Text = "Elapsed Time: " + System.Convert.ToString(TimerEnd); uCalc.ucReleaseItem(ExprHandle); uCalc.ucReleaseItem(xHandle); } |
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 3.0
· See also What’s New.
New or enhanced in version 2.96