DefineConstantMethod

Applies to:uCalc Transform
Class:uCalc
Defines a constant
Syntax
DefineConstant(Definition, VariableAddress)
Parameters
Definition
String
Text containing constant to be defined
VariableAddress
ADDR
(Default = 0)
Address the constant is attached to
Returns
Type: Item
Defines a constant
Remarks
A constant definition is similar to that of a variable defined with DefineVariable, except that the end-user is not allowed to modify the value of a constant. You are, however, allowed to perform lower-level operations such as renaming a constant, or deleting it and re-defining a new constant or variable with the same name but different value. If you attach the constant to the address of a variable in your host program, the host program may change the value. However, when the end-user attempts to assign a value to the constant, an error is raised.

DefineConstant("x") is the same as: Define("Lock ~~ Variable: x")

Example 1: Defining a constant

uc.DefineVariable("x = 10")
uc.DefineConstant("Pi = 3.14")

uc.EvalStr("x = 20")
Console.WriteLine(uc.ErrorMessage()) ' Returns "No error"

uc.EvalStr("Pi = 25")
Console.WriteLine(uc.ErrorMessage()) ' Returns "Value cannot be assigned here"

Console.WriteLine(uc.EvalStr("x")) ' Returns 20
Console.WriteLine(uc.EvalStr("Pi")) ' Returns 3.14

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
Console.WriteLine(uc.ErrorMessage()); // Returns "No error";

uc.EvalStr("Pi = 25");
Console.WriteLine(uc.ErrorMessage()); // Returns "Value cannot be assigned here";

Console.WriteLine(uc.EvalStr("x")); // Returns 20;
Console.WriteLine(uc.EvalStr("Pi")); // Returns 3.14;

uc.GetItemOf("Pi").Release();

          

      uc.DefineVariable('x = 10');
      uc.DefineConstant('Pi = 3.14');

      uc.EvalStr('x = 20');
      WriteLn(uc.ErrorMessage()); // Returns 'No error';

      uc.EvalStr('Pi = 25');
      WriteLn(uc.ErrorMessage()); // Returns 'Value cannot be assigned here';

      WriteLn(uc.EvalStr('x')); // Returns 20;
      WriteLn(uc.EvalStr('Pi')); // Returns 3.14;

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
cout << uc.ErrorMessage() << endl; // Returns "No error";

uc.EvalStr("Pi = 25");
cout << uc.ErrorMessage() << endl; // Returns "Value cannot be assigned here";

cout << uc.EvalStr("x") << endl; // Returns 20;
cout << uc.EvalStr("Pi") << endl; // Returns 3.14;

uc.GetItemOf("Pi").Release();

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
Console::WriteLine(uc.ErrorMessage()); // Returns "No error";

uc.EvalStr("Pi = 25");
Console::WriteLine(uc.ErrorMessage()); // Returns "Value cannot be assigned here";

Console::WriteLine(uc.EvalStr("x")); // Returns 20;
Console::WriteLine(uc.EvalStr("Pi")); // Returns 3.14;

uc.GetItemOf("Pi").Release();

          
Example 2: Bootstraping the Cos function definition to change it to Degree mode

uc.DefineConstant("pi = Atan(1) * 4")

' Default Radian mode
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns -1
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -0.59846

' Cos redefined for Degree mode
Dim Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)")
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns 0.998497
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -1

' Reverts back to original definition
Degree.Release()
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns -1
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -0.59846

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

// Cos redefined for Degree mode
var Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns 0.998497;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -1;

// Reverts back to original definition
Degree.Release();
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

          

         uc.DefineConstant('pi = Atan(1) * 4');

         // Default Radian mode
         WriteLn(uc.Eval('Cos(pi)')); // Returns -1;
         WriteLn(uc.Eval('Cos(180)')); // Returns -0.59846;

         // Cos redefined for Degree mode
//var Degree = uc.Define('Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)');
         WriteLn(uc.Eval('Cos(pi)')); // Returns 0.998497;
         WriteLn(uc.Eval('Cos(180)')); // Returns -1;

         // Reverts back to original definition
         Degree.Release();
         WriteLn(uc.Eval('Cos(pi)')); // Returns -1;
         WriteLn(uc.Eval('Cos(180)')); // Returns -0.59846;

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
cout << uc.Eval("Cos(pi)") << endl; // Returns -1;
cout << uc.Eval("Cos(180)") << endl; // Returns -0.59846;

// Cos redefined for Degree mode
auto Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
cout << uc.Eval("Cos(pi)") << endl; // Returns 0.998497;
cout << uc.Eval("Cos(180)") << endl; // Returns -1;

// Reverts back to original definition
Degree.Release();
cout << uc.Eval("Cos(pi)") << endl; // Returns -1;
cout << uc.Eval("Cos(180)") << endl; // Returns -0.59846;

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

// Cos redefined for Degree mode
auto Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns 0.998497;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -1;

// Reverts back to original definition
Degree.Release();
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

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

Private Function DefineConstant__(ByVal uCalcHandle As IntPtr,ByVal Definition As String , ByVal VariableAddress As IntPtr  ) As IntPtr
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="DefineConstant")]

protected static extern IntPtr DefineConstant_(IntPtr uCalcHandle, string Definition , IntPtr VariableAddress   );
            
{DLLImport}function DefineConstant__(uCalcHandle: System.Pointer;Definition: AnsiString ; VariableAddress: System.Pointer): System.Pointer; cdecl; external uCalcDLL name 'DefineConstant';

            
typedef uCalcPtr (* __DefineConstant)(void *uCalcHandle, CONSTCHAR Definition  ,  void * VariableAddress  ); 

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

static uCalcPtr DefineConstant_(void *  uCalcHandle, MARSHALSTR Definition  ,  void *   VariableAddress);
            
See also
Prev | Next