Visual Basic .NET

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

1. Copy uCalcVB.vb to your project directory, or an appropriate include directory.
2. Place ucFMP315.dll and uCalcLBx.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 uCalcVB.vb.

Now you're all set to go.

Long vs Integer

The 32-bit data type that is called Long in VB 6 as well as uCalc, is called Integer in VB.NET.  This distinction is very important to keep in mind, especially if you wish to deal with integer types in uCalc.

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

What’s new in Version 3

·         Dealing with callbacks is much easier.  No need to fiddle directly with delegates.  Simply use AddressOf