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.

Value(string)

Method

Product: 

Fast Math Parser

Class: 

Item

Sets the value of a variable by parsing and evaluating a string expression, automatically handling type conversion.

Syntax

Value(string)

Parameters

valueExpression
string
A string containing the expression to be parsed, evaluated, and assigned to the variable.

Return

void

Remarks

⚙️ Overview

This method provides a universal way to set the value of a variable by parsing and evaluating a string expression. Unlike type-specific setters (e.g., ValueInt32), this overload can assign a value to a variable of any data type, automatically handling the necessary parsing and type conversion.

The input string is not just a literal value; it is treated as a complete uCalc expression. This means it can contain calculations, function calls, or even references to other variables.

💡 Key Features

  • Expression Evaluation: The input string is processed by the uCalc engine. For example, passing "10 * 2" will set a numeric variable to 20.
  • Automatic Type Conversion: The result of the expression is automatically converted to the target variable's data type. For instance, if a variable is an Int32, setting its value with "4.75" will result in the integer 4. If an unsigned type like Byte is given a negative number, it will wrap around (e.g., -1 becomes 255).
  • Universal Applicability: This single method works for all data types, simplifying code that needs to handle dynamic or user-provided input.

🆚 Comparative Analysis

vs. Standard Type Parsing (e.g., C# int.Parse())

In most languages, converting a string to a value is a direct parsing operation (int.Parse("123")). This operation fails if the string is not a valid representation of that type. uCalc's method is more powerful because it treats the string as a full expression to be evaluated, not just parsed. This allows for runtime calculations to determine the final value.

vs. Direct Assignment

In a compiled language, you assign a pre-computed value: myVar = 5 * 2;. With this Value() overload, you provide the formula as a string: MyVar.Value("5 * 2");. This is fundamental for building applications where logic is determined at runtime, such as scripting engines or spreadsheet applications. It decouples the value assignment from the compile-time logic of the host application.

This method should be used for variables created with DefineVariable or Define.

Examples

Setting variables of any data type
				
					using uCalcSoftware;

var uc = new uCalc();
var Int32Var = uc.DefineVariable("Int32Var As Int32");
var ByteVar = uc.DefineVariable("ByteVar As Byte");
var StrVar = uc.DefineVariable("StrVar As String");
var SngVar = uc.DefineVariable("SngVar As Single");

Int32Var.Value("4.25"); // Will be converted to integer
ByteVar.Value("-1");    // Will be converted to unsigned byte
StrVar.Value("'Test'");
SngVar.Value("1 + 0.25");

Console.WriteLine(uc.Eval("Int32Var"));
Console.WriteLine(uc.Eval("ByteVar"));
Console.WriteLine(uc.EvalStr("StrVar"));
Console.WriteLine(uc.EvalStr("SngVar"));

				
			
4
255
Test
1.25
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto Int32Var = uc.DefineVariable("Int32Var As Int32");
   auto ByteVar = uc.DefineVariable("ByteVar As Byte");
   auto StrVar = uc.DefineVariable("StrVar As String");
   auto SngVar = uc.DefineVariable("SngVar As Single");

   Int32Var.Value("4.25"); // Will be converted to integer
   ByteVar.Value("-1");    // Will be converted to unsigned byte
   StrVar.Value("'Test'");
   SngVar.Value("1 + 0.25");

   cout << uc.Eval("Int32Var") << endl;
   cout << uc.Eval("ByteVar") << endl;
   cout << uc.EvalStr("StrVar") << endl;
   cout << uc.EvalStr("SngVar") << endl;

}
				
			
4
255
Test
1.25
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim Int32Var = uc.DefineVariable("Int32Var As Int32")
      Dim ByteVar = uc.DefineVariable("ByteVar As Byte")
      Dim StrVar = uc.DefineVariable("StrVar As String")
      Dim SngVar = uc.DefineVariable("SngVar As Single")
      
      Int32Var.Value("4.25") '// Will be converted to integer
      ByteVar.Value("-1")    '// Will be converted to unsigned byte
      StrVar.Value("'Test'")
      SngVar.Value("1 + 0.25")
      
      Console.WriteLine(uc.Eval("Int32Var"))
      Console.WriteLine(uc.Eval("ByteVar"))
      Console.WriteLine(uc.EvalStr("StrVar"))
      Console.WriteLine(uc.EvalStr("SngVar"))
      
   End Sub
End Module
				
			
4
255
Test
1.25