Visual Basic .NET

 

Adding uCalc Fast Math Parser to your VB.NET application

 

In order to implement uCalc Fast Math Parser in Visual Basic .NET, follow these steps:

 

  1. Copy uCalcNET.vb to your project directory, or an appropriate include directory.
  2. Place uCalc296.dll and ucFMP296.dll either in the Windows System directory, your project's Bin directory, or any suitable directory that is in the path.
  3. From the menu, go to Project / Add Existing Item..., and select uCalcNET.vb.

 

Now you're all set to go.

 

Another way to implement uCalc FMP with VB.NET is described in the .NET topic.

 

Note:  Visual Basic .NET files are found in the ucFMP296\VB.NET\ directory.

 

In VB, 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).

 

 

Demo program

 

The name of the uCalc demo solution file to load into VB.NET is DemoNET.sln.  The source code in the project demonstrates the essential features, especially as they relate to VB.NET.  DemoNET.vb contains the code for uCalc callback routines, and FormNET.vb contains the main part of the code, which calls uCalc routines.

 

 

Long vs Integer

 

The 32-bit data type that is called Long in VB classic, is called Integer in VB.NET.  This distinction is very important to keep in mind.

 

 

ucSetVariableValue (Variable attachments and managed code)

 

For a usage of ucSetVariableValue unrelated to managed code see this example.

 

Because managed code may move variable addresses at any time, it is not possible to attach a uCalc variable to the address of a variable in your VB.NET code.  Even if you pin the address, the pinning effect is temporary.  Therefore, you should use ucSetVariableValue for numbers or ucSetVariableValueStr for strings whenever you want to update the value of a uCalc variable that is associated with a VB.NET host program variable.  For instance here is a piece of VB Classic code, followed by its VB.NET equivalent:

 

VB Classic code

 

Dim x As Double, SumTotal As Double

Dim ExprHandle As Long, xHandle As Long

 

xHandle = ucDefineVariable("x", VarPtr(x))

ExprHandle = ucParse("x^2 + 5")  

  

For x = 1 To 2000000

   SumTotal = SumTotal + ucEvaluate(ExprHandle)

Next

 

 

VB.NET equivalent

 

Dim x As Double, SumTotal As Double

Dim ExprHandle As Integer, xHandle As Integer

 

xHandle = ucDefineVariable("x")

ExprHandle = ucParse("x^2 + 5")

 

For x = 1 To 2000000

   ucSetVariableValue(xHandle, x)

   SumTotal = SumTotal + ucEvaluate(ExprHandle)

Next

 

The more complete example can be found here.

 

 

Callbacks under managed code

 

uCalc callbacks under VB.NET's managed code are handled quite differently than from under VB classic and the other compilers supported by uCalc.  In VB classic and other compilers, you can directly pass a function's address to uCalc.  In VB.NET, you must use delegates for callbacks.  With the other compilers, you have the option of using native or non-native callbacks.  Non-native callbacks allow you to use various non-generic data types, and you can choose to pass arguments by value or by reference.  In VB.NET you can only define native callbacks.  Since native callbacks are the only ones supported in VB.NET, the keyword Native is optional.

 

 

Examples:

 

The following examples are for key features that may have some peculiarities in VB.NET.  There are many more examples in Visual Basic (classic) that are close enough to VB.NET.

 

Example 1:  Simple evaluation with ucEvalStr

Example 2:  Fast evaluation millions of times in a loop

Example 3:  Defining a centralized error handler

Example 4:  Raising an error with ucRaiseErrorMessage

Example 5:  A native function callback with two numeric arguments

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

Example 7:  A native string callback function

 

See More Examples

 

 

Upgrading from version 2.95 to 2.96