Forum Discussion

amithsid's avatar
amithsid
Contributor
12 years ago

How to use private keyword in TestComplete?

I want to use the private keyword in JScript on TC.

What is the syntax to be used?

I don't want variables to be accessed across different units.

4 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi amith, unfortunately there is no way to declare a member private in a JScript or a Script Unit.


     


    There only two places in Test Complete where private members can be defined:


     


    1. In a Script Extension where access to members is controlled by the Description.xml, this has the disadvantage in that a Script Extension must be entirely self contained and cannot reference other Script Units


     


    2. In a JScript Closure using the Module pattern


     


    var MyModule = (function () {


      var priv = 123; // Private Var


      return {


        func: function( value ) { // Public function


          priv = priv + value;


          return priv;


        }


      };


    })();


     


    Which can be called:


     


    Log.Message( MyModule.func( 123 ) );


     


    The Module pattern has the disadvantage that none of the public members are available in the intellisense.


     


    When I first started using Test Complete, I initially tried using the Module pattern and worried about privates etc.


     


    Eventually, I gave up as it was just to much effort.


     


    Now we rely on documentation, developer discipline and using fully qualified member names, i.e. <ScriptUnit>.<Member>, to call external members on all occasions and don't rely on privates at all.


     


    Regards,


    Phil Baird

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Prasad, the Private Public access scope you describe is specific to VBScript.


     


    The OP of this thread is enquiring about Private keyword in JScript, which has no concept of Private and Public access scope.


     


    Regards,


    Phil Baird

  • Private variables are declared in the same way you declare a public variable. instead of public, use private keyword where you declare the variables.