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

   igot = initialisers(iwant)

end sub


function initialisers(reqd() )

   redim objs(1) ' declaring an object array

   redim retn(ubound(reqd))



   set objs(0) = someapp.someObject

   set objs(1) = someapp.anotherObject



   set retn(i)=objs(reqd(i))



   for i=0 to ubound(reqd)

      set retn(i) = objs(reqd(i))

   next



initialisers = retn




end function



When I debug and check the objs(0) and objs(1)they are shown as objects, however the objs(1) is am empty object, but objs(0) has the value I assigned to  it!



Any pointers/suggestions?






 



  • 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


  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)


    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


  • Thanks for trying that out.

    Can you see if this is an issue with Smartbear?



    Unit1.svb

    sub test

    'USEUNIT unit2

    somevariable = unit2.initialisers()

    end sub



    Unit2.svb

    function initilalisers()

    dim a(2)



    'fill the a() array with some objects.



    initialisers = a



    end function  ---> set a break point here



    now try evaluating the initilasers variable, you should see the window with objects list.

    But if you dig deeper you'll find an error that goes something like 'invalid ubound()'



    although there won't be any error if you run further and the values still get passed to the caller.



    Could be a bug. But my issue is resolved.

    Thanks for ur support again.





  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Glad to hear your issue is solved!



    By the way, I don't see the "invalid ubound" error with the multi-unit code you posted. As I've said before, the error probably comes from the specific objects you're saving to the array. But it's hard to tell for sure without knowing what exactly you're using in place of "fill the a() array with some objects". If you want to troubleshoot further, you can send your TestComplete project and the screenshots/video showing the problem to our Customer Care team, and they’ll look into it.