A practical example of `ByRef` to create a classic `Swap` function that modifies its arguments in the caller's scope.
ID: 1248
using uCalcSoftware;
var uc = new uCalc();
static void SwapValues(uCalc.Callback cb) {
// Get the item handles for the two variables passed by reference
var item1 = cb.ArgItem(1);
var item2 = cb.ArgItem(2);
// Use the item's DataType object to perform a highly efficient, pointer-based swap
item1.DataType.SwapScalarValues(item1.ValueAddr(), item2.ValueAddr());
}
// Define the Swap function with ByRef parameters
uc.DefineFunction("Swap(ByHandle a, ByHandle b)", SwapValues);
// Define the variables to be swapped
uc.DefineVariable("x = 100");
uc.DefineVariable("y = 200");
Console.WriteLine($"Before: x = {uc.Eval("x")}, y = {uc.Eval("y")}");
// Call the swap function
uc.Eval("Swap(x, y)");
Console.WriteLine($"After: x = {uc.Eval("x")}, y = {uc.Eval("y")}");
Before: x = 100, y = 200
After: x = 200, y = 100 using uCalcSoftware; var uc = new uCalc(); static void SwapValues(uCalc.Callback cb) { // Get the item handles for the two variables passed by reference var item1 = cb.ArgItem(1); var item2 = cb.ArgItem(2); // Use the item's DataType object to perform a highly efficient, pointer-based swap item1.DataType.SwapScalarValues(item1.ValueAddr(), item2.ValueAddr()); } // Define the Swap function with ByRef parameters uc.DefineFunction("Swap(ByHandle a, ByHandle b)", SwapValues); // Define the variables to be swapped uc.DefineVariable("x = 100"); uc.DefineVariable("y = 200"); Console.WriteLine($"Before: x = {uc.Eval("x")}, y = {uc.Eval("y")}"); // Call the swap function uc.Eval("Swap(x, y)"); Console.WriteLine($"After: x = {uc.Eval("x")}, y = {uc.Eval("y")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call SwapValues(uCalcBase::Callback cb) {
// Get the item handles for the two variables passed by reference
auto item1 = cb.ArgItem(1);
auto item2 = cb.ArgItem(2);
// Use the item's DataType object to perform a highly efficient, pointer-based swap
item1.DataType().SwapScalarValues(item1.ValueAddr(), item2.ValueAddr());
}
int main() {
uCalc uc;
// Define the Swap function with ByRef parameters
uc.DefineFunction("Swap(ByHandle a, ByHandle b)", SwapValues);
// Define the variables to be swapped
uc.DefineVariable("x = 100");
uc.DefineVariable("y = 200");
cout << "Before: x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl;
// Call the swap function
uc.Eval("Swap(x, y)");
cout << "After: x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl;
}
Before: x = 100, y = 200
After: x = 200, y = 100 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call SwapValues(uCalcBase::Callback cb) { // Get the item handles for the two variables passed by reference auto item1 = cb.ArgItem(1); auto item2 = cb.ArgItem(2); // Use the item's DataType object to perform a highly efficient, pointer-based swap item1.DataType().SwapScalarValues(item1.ValueAddr(), item2.ValueAddr()); } int main() { uCalc uc; // Define the Swap function with ByRef parameters uc.DefineFunction("Swap(ByHandle a, ByHandle b)", SwapValues); // Define the variables to be swapped uc.DefineVariable("x = 100"); uc.DefineVariable("y = 200"); cout << "Before: x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl; // Call the swap function uc.Eval("Swap(x, y)"); cout << "After: x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub SwapValues(ByVal cb As uCalc.Callback)
'// Get the item handles for the two variables passed by reference
Dim item1 = cb.ArgItem(1)
Dim item2 = cb.ArgItem(2)
'// Use the item's DataType object to perform a highly efficient, pointer-based swap
item1.DataType.SwapScalarValues(item1.ValueAddr(), item2.ValueAddr())
End Sub
Public Sub Main()
Dim uc As New uCalc()
'// Define the Swap function with ByRef parameters
uc.DefineFunction("Swap(ByHandle a, ByHandle b)", AddressOf SwapValues)
'// Define the variables to be swapped
uc.DefineVariable("x = 100")
uc.DefineVariable("y = 200")
Console.WriteLine($"Before: x = {uc.Eval("x")}, y = {uc.Eval("y")}")
'// Call the swap function
uc.Eval("Swap(x, y)")
Console.WriteLine($"After: x = {uc.Eval("x")}, y = {uc.Eval("y")}")
End Sub
End Module
Before: x = 100, y = 200
After: x = 200, y = 100 Imports System Imports uCalcSoftware Public Module Program Public Sub SwapValues(ByVal cb As uCalc.Callback) '// Get the item handles for the two variables passed by reference Dim item1 = cb.ArgItem(1) Dim item2 = cb.ArgItem(2) '// Use the item's DataType object to perform a highly efficient, pointer-based swap item1.DataType.SwapScalarValues(item1.ValueAddr(), item2.ValueAddr()) End Sub Public Sub Main() Dim uc As New uCalc() '// Define the Swap function with ByRef parameters uc.DefineFunction("Swap(ByHandle a, ByHandle b)", AddressOf SwapValues) '// Define the variables to be swapped uc.DefineVariable("x = 100") uc.DefineVariable("y = 200") Console.WriteLine($"Before: x = {uc.Eval("x")}, y = {uc.Eval("y")}") '// Call the swap function uc.Eval("Swap(x, y)") Console.WriteLine($"After: x = {uc.Eval("x")}, y = {uc.Eval("y")}") End Sub End Module