This topic describes methods of calling items declared in another unit with C#Script or C++Script code.
Adding Script References
To reference another unit from the current unit using C#Script or C++Script, add the following line at the beginning of the current script (before any functions and variable declarations):
// To reference a single unit
// To reference multiple units
Calling Items From Another Unit
Once an external unit is linked to the current one, all the routines, as well as global variables and constants of the external unit, can be called in the current unit. It is recommended, though not obligatory, to prefix the names of external routines, variables and constants with the unit's name. That is, in general, calling items from another unit with any of the following code instructions is correct:
However, you must specify the name of the referenced unit in the following cases:
When the current unit already contains a routine, global variable or global constant with the same name. Without prefixing, the routine, variable or constant of the current unit will be called.
When the name of an external routine is the same as the name of an external unit. Calling such a routine without a prefix will cause an exception.
To call a routine defined in an external unit that is linked to your current unit, you can also pass a string that specifies the target routine’s full name (that is, "unit_name.routine_name") to the
eval function. If the external routine you want to call takes parameters, include them into the string parameter you pass to the eval functions.
eval("UnitB['TestFunction'](1, 'string', true)");
result = eval("UnitB['TestFunction']()");
To use common prototype methods of JScript, C#Script and C++Script from referenced units, you need to create wrapper functions for them. For details, see JScript, C#Script and C++Script - Specifics of Usage.
To make a variable or constant visible to other units, declare it as global, that is, outside any routines.
If the current unit is linked to a unit that is linked to another unit, the script declared in the third unit can be called from the current unit. For instance, if you need to call a function declared in UnitA from UnitC, which is linked to UnitB, you must add references to UnitA in UnitB:
Log["Message"]("The procedure is run.");
USEUNITstatement does not import, share or merge project items. So, these elements remain available only for the project the unit is imported from. To be able to work with elements that reside within external projects, you may need to import them to or share them with the current project that uses the unit. For more information about this, see Sharing Project Items and Their Child Elements Among Several Projects.
If you reference a script from another project, and that script uses object names from Name Mapping, you may need to merge the external Name Mapping file with your local Name Mapping file. For instructions, see Merge Name Mapping Files.
In C#Script and C++Script, when a routine is called from another script unit, the caller property of the
Functionobject that corresponds to the current routine returns
null. Normally, when a function is called by another routine declared in the same script unit, the caller property returns a reference to the function that invoked the current function. If the caller property is used in a string context, it contains the text of the function that called the current routine. See the description of the
Functionobject and its caller property in the MSDN Library.
C#Script and C++Script do not allow the use of circular references. They may cause errors in the jscript.dll library (C#Script and C++Script are based on JScript and also use this DLL) and these errors can cause other errors in TestComplete.