Raises an error with a dynamic message if a validation check fails within a callback.
ID: 483
using uCalcSoftware;
var uc = new uCalc();
static void ValidateValue(uCalc.Callback cb) {
var val = cb.Arg(1);
if (val > 100) {
// The error message includes the problematic value, making it dynamic.
cb.Error.Raise("Value exceeds maximum of 100. Got: " + val.ToString());
} else {
cb.Return(val);
}
}
uc.DefineFunction("CheckValue(val)", ValidateValue);
Console.WriteLine(uc.EvalStr("CheckValue(50)"));
Console.WriteLine(uc.EvalStr("CheckValue(123)"));
50
Value exceeds maximum of 100. Got: 123 using uCalcSoftware; var uc = new uCalc(); static void ValidateValue(uCalc.Callback cb) { var val = cb.Arg(1); if (val > 100) { // The error message includes the problematic value, making it dynamic. cb.Error.Raise("Value exceeds maximum of 100. Got: " + val.ToString()); } else { cb.Return(val); } } uc.DefineFunction("CheckValue(val)", ValidateValue); Console.WriteLine(uc.EvalStr("CheckValue(50)")); Console.WriteLine(uc.EvalStr("CheckValue(123)"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call ValidateValue(uCalcBase::Callback cb) {
auto val = cb.Arg(1);
if (val > 100) {
// The error message includes the problematic value, making it dynamic.
cb.Error().Raise("Value exceeds maximum of 100. Got: " + to_string((int)val));
} else {
cb.Return(val);
}
}
int main() {
uCalc uc;
uc.DefineFunction("CheckValue(val)", ValidateValue);
cout << uc.EvalStr("CheckValue(50)") << endl;
cout << uc.EvalStr("CheckValue(123)") << endl;
}
50
Value exceeds maximum of 100. Got: 123 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call ValidateValue(uCalcBase::Callback cb) { auto val = cb.Arg(1); if (val > 100) { // The error message includes the problematic value, making it dynamic. cb.Error().Raise("Value exceeds maximum of 100. Got: " + to_string((int)val)); } else { cb.Return(val); } } int main() { uCalc uc; uc.DefineFunction("CheckValue(val)", ValidateValue); cout << uc.EvalStr("CheckValue(50)") << endl; cout << uc.EvalStr("CheckValue(123)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub ValidateValue(ByVal cb As uCalc.Callback)
Dim val = cb.Arg(1)
If val > 100 Then
'// The error message includes the problematic value, making it dynamic.
cb.Error.Raise("Value exceeds maximum of 100. Got: " + val.ToString())
Else
cb.Return(val)
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("CheckValue(val)", AddressOf ValidateValue)
Console.WriteLine(uc.EvalStr("CheckValue(50)"))
Console.WriteLine(uc.EvalStr("CheckValue(123)"))
End Sub
End Module
50
Value exceeds maximum of 100. Got: 123 Imports System Imports uCalcSoftware Public Module Program Public Sub ValidateValue(ByVal cb As uCalc.Callback) Dim val = cb.Arg(1) If val > 100 Then '// The error message includes the problematic value, making it dynamic. cb.Error.Raise("Value exceeds maximum of 100. Got: " + val.ToString()) Else cb.Return(val) End If End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("CheckValue(val)", AddressOf ValidateValue) Console.WriteLine(uc.EvalStr("CheckValue(50)")) Console.WriteLine(uc.EvalStr("CheckValue(123)")) End Sub End Module