uCalc API Version: 2.1.3-preview.2 Released: 6/17/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

SwapScalarValues

Method

Product: 

Fast Math Parser

Class: 

DataType

Efficiently swaps the values of two uCalc variables of the same underlying data type.

Syntax

SwapScalarValues(POINTER, POINTER)

Parameters

pointerA
POINTER
The memory address of the first variable's value, obtained via `Variable.ValueAddr()`.
pointerB
POINTER
The memory address of the second variable's value, obtained via `Variable.ValueAddr()`.

Return

void

This method does not return a value.

Remarks

⚙️ Functionality

This method performs a highly efficient swap of two variable values. Instead of copying the actual data from one variable to another (which can be slow for large strings or complex objects), it swaps the internal pointers that point to the data. This makes the operation nearly instantaneous regardless of the data size.

To use this method, you must first obtain a DataType object corresponding to the type of variables you wish to swap. Both variables must be of the same type.

🆚 Comparative Analysis

In most high-level languages, swapping variables is straightforward:

  • C# (Tuple Deconstruction): (a, b) = (b, a);
  • Traditional (Temp Variable): temp = a; a = b; b = temp;

The uCalc SwapScalarValues method is a lower-level operation. While the syntax is more verbose because it requires getting the DataType and value pointers, it provides a direct hook into the engine's memory management. This approach guarantees the most performant swap possible within the uCalc ecosystem, mirroring the efficiency of pointer manipulation in languages like C++.

Usage Example:

// 1. Define variablesvar a = uc.DefineVariable("a = 10");var b = uc.DefineVariable("b = 20");// 2. Get the DataType for integersvar intType = uc.DataTypeOf("integer");// 3. Swap using their value addressesintType.SwapScalarValues(a.ValueAddr(), b.ValueAddr());

⚠️ Warning: Both variables involved in the swap must be of the same data type associated with the DataType object. Attempting to swap values of incompatible types (e.g., an integer with a string) will result in undefined behavior, potentially leading to memory corruption or application instability.

Examples

SwapScalarValues
				
					using uCalcSoftware;

var uc = new uCalc();
var MyVar1 = uc.DefineVariable("MyVar1 = 123.456");
var MyVar2 = uc.DefineVariable("MyVar2 = 654.321");
var MyStr1 = uc.DefineVariable("MyStr1 = 'First string'");
var MyStr2 = uc.DefineVariable("MyStr2 = 'Second string'");

Console.WriteLine(uc.EvalStr("MyVar1"));
Console.WriteLine(uc.EvalStr("MyVar2"));
Console.WriteLine(uc.EvalStr("MyStr1"));
Console.WriteLine(uc.EvalStr("MyStr2"));
Console.WriteLine("---");

uc.DataTypeOf("double").SwapScalarValues(MyVar1.ValueAddr(), MyVar2.ValueAddr());
uc.DataTypeOf("string").SwapScalarValues(MyStr1.ValueAddr(), MyStr2.ValueAddr());

Console.WriteLine(uc.EvalStr("MyVar1")); // Values of MyVar1 and MyVar2 are now swapped
Console.WriteLine(uc.EvalStr("MyVar2"));
Console.WriteLine(uc.EvalStr("MyStr1")); // Values of MyStr1 and MyStr2 are now swapped
Console.WriteLine(uc.EvalStr("MyStr2"));
				
			
123.456
654.321
First string
Second string
---
654.321
123.456
Second string
First string
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto MyVar1 = uc.DefineVariable("MyVar1 = 123.456");
   auto MyVar2 = uc.DefineVariable("MyVar2 = 654.321");
   auto MyStr1 = uc.DefineVariable("MyStr1 = 'First string'");
   auto MyStr2 = uc.DefineVariable("MyStr2 = 'Second string'");

   cout << uc.EvalStr("MyVar1") << endl;
   cout << uc.EvalStr("MyVar2") << endl;
   cout << uc.EvalStr("MyStr1") << endl;
   cout << uc.EvalStr("MyStr2") << endl;
   cout << "---" << endl;

   uc.DataTypeOf("double").SwapScalarValues(MyVar1.ValueAddr(), MyVar2.ValueAddr());
   uc.DataTypeOf("string").SwapScalarValues(MyStr1.ValueAddr(), MyStr2.ValueAddr());

   cout << uc.EvalStr("MyVar1") << endl; // Values of MyVar1 and MyVar2 are now swapped
   cout << uc.EvalStr("MyVar2") << endl;
   cout << uc.EvalStr("MyStr1") << endl; // Values of MyStr1 and MyStr2 are now swapped
   cout << uc.EvalStr("MyStr2") << endl;
}
				
			
123.456
654.321
First string
Second string
---
654.321
123.456
Second string
First string
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim MyVar1 = uc.DefineVariable("MyVar1 = 123.456")
      Dim MyVar2 = uc.DefineVariable("MyVar2 = 654.321")
      Dim MyStr1 = uc.DefineVariable("MyStr1 = 'First string'")
      Dim MyStr2 = uc.DefineVariable("MyStr2 = 'Second string'")
      
      Console.WriteLine(uc.EvalStr("MyVar1"))
      Console.WriteLine(uc.EvalStr("MyVar2"))
      Console.WriteLine(uc.EvalStr("MyStr1"))
      Console.WriteLine(uc.EvalStr("MyStr2"))
      Console.WriteLine("---")
      
      uc.DataTypeOf("double").SwapScalarValues(MyVar1.ValueAddr(), MyVar2.ValueAddr())
      uc.DataTypeOf("string").SwapScalarValues(MyStr1.ValueAddr(), MyStr2.ValueAddr())
      
      Console.WriteLine(uc.EvalStr("MyVar1")) '// Values of MyVar1 and MyVar2 are now swapped
      Console.WriteLine(uc.EvalStr("MyVar2"))
      Console.WriteLine(uc.EvalStr("MyStr1")) '// Values of MyStr1 and MyStr2 are now swapped
      Console.WriteLine(uc.EvalStr("MyStr2"))
   End Sub
End Module
				
			
123.456
654.321
First string
Second string
---
654.321
123.456
Second string
First string