Forum Discussion

googleid_104927's avatar
googleid_104927
Contributor
11 years ago

Return an object array in vbscript

Hi, I am trying to return an object array, but somehow only the first element gets initilaised. The code is below: sub driver    dim iwant(1)    iwant(0)=0    iwant(1)=1  ...
  • HKosova's avatar
    11 years ago


    Hi TestComplete User,



    Your code works fine for me. I replaced someapp.someObject -> Sys.Desktop, someapp.anotherObject -> Sys.OSInfo, and, also, removed the superfluous "set retn(i) =" outside the loop.





    Maybe some array elements aren't initialized because their corresponding objects don't exist? Are there any errors in the test log? What's your complete code including the object names?



    By the way, here's the simpler way to populate an object array using a Dictionary object:



    Sub driver

      igot = initialisers

    End Sub



    Function initialisers

      Dim dic

      Set dic = CreateObject("Scripting.Dictionary")



      Set dic(0) = Sys

      Set dic(1) = Sys.Desktop

      Set dic(2) = Sys.Process("explorer")



      initialisers = dic.Items

    End Function



    Or, you could use VBScript's Array function:



    Sub driver

      igot = initialisers

    End Sub



    Function initialisers

      Dim arr

      arr = Array(_

        Sys, _

        Sys.Desktop, _

        Sys.Process("explorer")

      )



      initialisers = arr

    End Function