Forum Discussion

MulgraveTester's avatar
MulgraveTester
Frequent Contributor
15 years ago

VB Variables - defining type and using global

1) Why is there no way to define the type of a variable in VBScript? I can in VBA. I would like to define more complex objects and tables.


dim myvar as integer


2) In your help file under "Supported Scripting Languages - Specifics of Usage" you say "Variables that are declared with the Dim statement within a routine are only visible within that routine. Variables that are used without declaring them with Dim, become global variables."



When I try and use this behaviour it has no effect? Is the statement correct? The following code logs the number 5. If the statement is true then it should log 10.



sub childSub

    tbControlUtility = 10  

end sub


sub parentSub

    tbControlUtility = 5 

    call childSub()

    log.message("tbControlUtility = " & tbControlUtility)

end sub




4 Replies

  • karkadil's avatar
    karkadil
    Valued Contributor
    1) Because VBScript isn't a VBA, they are two different languages and you can't expect the same behavior from 2 different languages. If you need complex objects, try using Class statement. Here is documentation on VBScript

    http://msdn.microsoft.com/en-us/library/t0aew7h6



    2) Well, it looks like help topic isn't clear enough. The point is, that if you declare a variable outside a procedure and the usen variable with the same name inside a procedure, but without using Dim statement, then global variable will be used instead. Here is a modified example from TC help:



    Dim i

    Sub ChildSub

      For i = 0 To 5

        Log.Message("child i = " & i)

      Next

    End Sub



    Sub ParentSub

      ' THIS LOOP WILL NEVER STOP, I CHECKED!

      For i = 0 To 10

        ChildSub

        Log.Message("parent i = " & i)

      Next

    End Sub


  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Generally speaking, most "Script" languages (JScript, VBScript, DelphiScript, C#Script, etc) are non-typed languages.  All variables are, essentially, OleVariants in use in the TestComplete environment.  This is just one of the parts of using a scripting language and is not unique to TestComplete.
  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    >  All variables are, essentially, OleVariants in use in the TestComplete environment.

    This is not related to TC environment, but all variables in all scripting languages are of OleVariant type because of the requirement to be COM-compatible.

    And the actual type of the data that the given variable contains at the given time during runtime is stored within OleVariant structure and can be determined by using VarType() function for VBScript and the similar functions in other scripting languages.





    The above is equivalent to declaring the variable in VBA like

    Dim obj As Object

    In this case these lines will work during runtime:

    Set obj = CreateObject("Excel.Application")

    Set obj = CreateObject("Word.Application")



    but if the variable is declared as

    Dim obj As Excel.Application

    only the first line will succeed while the second will trigger compilation error.





    Excuse me for jumping in, but just my two cents... :)
  • It is true that this is the nature of VBSCRIPT not Test Complete. It was the same in ASP (pre-asp.net aka asp classic). It is what it is.