Forum Discussion

OAn's avatar
OAn
Contributor
5 years ago
Solved

Declare and initialize a global variable

Environment: TestComplete 14.30.3743.7 x64 with Delphi script

 

At function level a variable can be declared and initialized at the same time: the syntax is as follow

var i:integer=8;

 

Using the same syntax for a global variable will lead to a TC syntax error. Does anyone know if it is possible to initialize a global variable with a default value? or, is this allowed within the above mentioned environment?

  • Funny... I tried that syntax inside a function declaration and got a syntax error as well.

     

    As I recall, when making variable declarations that are NOT a parameter of the function, you cannot declare a default value.

     

    For example, THIS is allowed

     

    function blah(i:integer =8):Variant;
    
    begin
        
    end;
    

    while this is not

     

    function blah:Variant;
    var 
    i:integer = 8; begin end;

    It's been a while since I've done DelphiScript work... but I do know that it's different than Delphi.  While Delphi proper allows for assigning default values to global variables, I don't think that applies to the DelphiScript implementation in TestComplete.  There really isn't a "global" scope in a TestComplete project of script code so it doesn't apply.

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Funny... I tried that syntax inside a function declaration and got a syntax error as well.

     

    As I recall, when making variable declarations that are NOT a parameter of the function, you cannot declare a default value.

     

    For example, THIS is allowed

     

    function blah(i:integer =8):Variant;
    
    begin
        
    end;
    

    while this is not

     

    function blah:Variant;
    var 
    i:integer = 8; begin end;

    It's been a while since I've done DelphiScript work... but I do know that it's different than Delphi.  While Delphi proper allows for assigning default values to global variables, I don't think that applies to the DelphiScript implementation in TestComplete.  There really isn't a "global" scope in a TestComplete project of script code so it doesn't apply.