Return an object array in vbscript
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