Forum Discussion

bknutson's avatar
bknutson
Contributor
2 years ago
Solved

Unit level variable declaration

I have a quick question on variable declaration that I haven't been able to find an answer on. 

For a VBScript example, in a Unit, we would put a declaration in a Subroutine such "Dim objectX".

 

I see that Test Complete allows me to put this declaration outside of any subroutine/function in a Unit and also set it.  This seems to allow this declaration (i.e. "objectX") to be used for every subroutine/function within the Unit (like a global unit variable) instead of setting it in each subroutine/function. 

 

My question is if this is acceptable to do?  Can variables be declared outside of subroutines/functions at the Unit level?  It appears to work but, maybe it isn't suggested?  Are there any drawbacks or 'gotchas' if it is allowed?   

 

Thanks

2 Replies

    • bknutson's avatar
      bknutson
      Contributor

      Thanks Marsha.

       

      I will look further at the links you provided.

       

      I don't see off-hand it referring to what I was trying but, I will continue to look.  I was just trying to see if there is anything that would give me troubles by doing this. 

       

      I did notice one thing.  I tried having a Script Variable set in a Unit outside of any routine and I was setting that variable using a Persistent Project Variable.  The Project Variable is set in the 'OnStart' General Event.  When the script is run, the Script Variable is set before the 'OnStart' section is run.  So, in this case, the Script Variable would get set with the 'old' value.

       

      If I set the Script Variable from within a routine in the Unit, the 'OnStart' section will run first, then the Script Variable gets set with the new value.

       

      Small thing but figured I would mention it.  I found that setting a variable, such as a process, outside of any routines saves some time.  I will probably just use this sparingly.

       

      Thanks again.  I will mark your solution as an accepted.  This may not be done too often.