Forum Discussion

ngalchenko's avatar
ngalchenko
New Contributor
12 years ago

How to use Consts in TestComplete (JScript) ?


I need to declare constants in my JScript code.



1. I tried this code: Consts TestID=50;

Program breaks on this line and I am getting compilation error.



2. I tried const TestID=50;

In this case I am getting syntax error.



Please help



 



 



 



 





 



 



 



 



 


  • Hi Natalya.



    JScript in TC does not support constants.



    You may use either variables or functions which return a value.



  • Hi Natalya.



    JScript in TC does not support constants.



    You may use either variables or functions which return a value.



  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Just as a matter of interest, a cludge I put together utilising JScript Closures to provides access to constant, unchangeable values.




    // Module that provides access to Constant, unchangeable, values



    var constant = (function() {


      


      // The Constant values, as these are in a Closure, they


      // cannot be changed unless a method was to be provided


      var CONST = "I am Constant",


          CONST_TWO = "As am I";  


     


      return {


        // Returns the value of the Constant specified by the name parameter


        value: function( name ) {


          try {


            // Use our friend eval to get the constant value as we referencing it by


            // a String. We can't just use eval( "return " + name ); as it will throw a


            // "'return' statement outside of function" exception        


            eval( "var val = " + name );


            return val;


          } catch( e ) {


            // Specified Constant does not exist so just return null


            return null;


          }


        } 


      }


    })();



     


    // Tests the constant Module


    function testConstant() {


      var s = constant.value( "CONST" );


      Log.Message( s ); // Logs I am Constant


      Log.Message( constant.value( "CONST_TWO" ) ); // Logs As am I


      Log.Message( constant.value( "DOES_NOT_EXIST" ) ); // Logs a null String 


    }



    It may be of interest to someone.



    Regards,

    Phil Baird