Forum Discussion

amithsid's avatar
amithsid
Contributor
11 years ago
Solved

TC is accessing public variables across scripts

Hello,



I have many units in the same folder under a script of a project. Inside a folder, many units have to access the public variables declared in that particular unit.

The problem is when I run a script; TC tries to search for another public variable declared in a different script under the same folder, what is the solution?



Thank you,

Amith n g
  • Hi amith, when using multiple Script Units that may have name clashes, I would recommend using the fully qualified name, i,e, <ScriptUnit>.<MemberName>, as described in the Using Methods and Variables Defined in Another Unit documentation:




    "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, any of the following code instructions is correct:


     


    MyExtUnit.MyRoutine(Param1, Param2)


    MyRoutine(Param1, Param2)


     


    MyExtUnit.MyVariable


    MyVariable


     


    MyExtUnit.MyConstant


    MyConstant


     


    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.


    • For VBScript and DelphiScript: when calling routines, variables or constants declared in a third unit."





    In fact, our coding standard is that any member that is called from another Script Unit, regardless of a clash existing or not, will ALWAYS be called using the fully qualified name

     


    Regards,


    Phil Baird

15 Replies

  • googleid_109910's avatar
    googleid_109910
    Occasional Contributor
    Hi Phil,

    The problem we are facing is as follows,

    We are maintaining different scripts for each screen (ex: script1 and script2), but in the same project. In each script we are declaring objects of that particular screen as global variables. So if i want to run script 2, it throws an error that objects in script 1 are not found. I want objects or variables in script 1 should be local to that script only. Is there any way to restrict the scope of those variables to that script itself? 
  • Hey Phil,



    That is what is exactly happening, Test Comple appears to parse and evaluate every Script Unit in a Project at the start of Test execution, even if they are not involved in the Test being run. How to stop it?



    Thank you,

    Amith n g
  • Hello,



    Oh ok, thanks for notifying. This can be stopped if I declare the variables inside the function. But it demands for an extra function call whenever we want to use it.



    Thank you,

    Amith n g

  • googleid_109910's avatar
    googleid_109910
    Occasional Contributor
    Hi Phill,

    Thanks for the update on that. And also is there any way to make the global variables declared in one script to be local to that script only?







    Thanks

    Raghavendra
  • Thank you Prasad,



    Can you show us an example of the method you have described using JScript?



    Regards,

    Amith n g