SetVariableValueAnyMethod

Applies to:Fast Math Parser
Class:uCalc.Item
Sets the value of a variable of any data type by passing a string
Syntax
SetVariableValueAny(Value)
Parameters
Value
String
String containing the value that will be converted and stored in the variable
Returns
Nothing
Remarks
This lets you set the value of a variable defined with DefineVariable (or Define) regardless of the variable's data type. The value is passed as a string, and internally converted to the appropriate data type. Note: Because a conversion takes place with SetVariableValueAny, it is typically slower than calling SetVariableValue for floating point, or one of the other versions of SetVariableValue specific to a data type.
Example 1: Setting variables of any data type

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.SetVariableValueAny("4.25"): ' Will be converted to integer
ByteVar.SetVariableValueAny("-1"): ' Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'")
SngVar.SetVariableValueAny("1.25")

Console.WriteLine(uc.Eval("Int32Var")) ' Returns 4
Console.WriteLine(uc.Eval("ByteVar")) ' Returns 255
Console.WriteLine(uc.EvalStr("StrVar")) ' Returns "Test"
Console.WriteLine(uc.EvalStr("SngVar")) ' Returns 1.25

          

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.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

Console.WriteLine(uc.Eval("Int32Var")); // Returns 4;
Console.WriteLine(uc.Eval("ByteVar")); // Returns 255;
Console.WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
Console.WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

          

//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.SetVariableValueAny('4.25'); // Will be converted to integer
      ByteVar.SetVariableValueAny('-1');    // Will be converted to unsigned byte
      StrVar.SetVariableValueAny('"Test"');
      SngVar.SetVariableValueAny('1.25');

      WriteLn(uc.Eval('Int32Var')); // Returns 4;
      WriteLn(uc.Eval('ByteVar')); // Returns 255;
      WriteLn(uc.EvalStr('StrVar')); // Returns 'Test';
      WriteLn(uc.EvalStr('SngVar')); // Returns 1.25;

          

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.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

cout << uc.Eval("Int32Var") << endl; // Returns 4;
cout << uc.Eval("ByteVar") << endl; // Returns 255;
cout << uc.EvalStr("StrVar") << endl; // Returns "Test";
cout << uc.EvalStr("SngVar") << endl; // Returns 1.25;

          

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.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

Console::WriteLine(uc.Eval("Int32Var")); // Returns 4;
Console::WriteLine(uc.Eval("ByteVar")); // Returns 255;
Console::WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
Console::WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="SetVariableValueAny")> _

Private Sub SetVariableValueAny__(ByVal ItemHandle As IntPtr,ByVal Value As String)
End Sub
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="SetVariableValueAny")]

protected static extern  void SetVariableValueAny_(IntPtr ItemHandle, string Value);
            
{DLLImport}procedure SetVariableValueAny__(ItemHandle: System.Pointer;Value: AnsiString); cdecl; external uCalcDLL name 'SetVariableValueAny';

            
typedef  void (* __SetVariableValueAny)(void *ItemHandle, CONSTCHAR Value ); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "SetVariableValueAny")]

static void SetVariableValueAny_(void *  ItemHandle, MARSHALSTR Value);
            
See also
Prev | Next