Page 1 of 1 1
Topic Options
#207303 - 2013-05-12 08:58 PM Treating an object as an Array
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Treating an object as an Array

This is not a new 'problem', maybe not even a problem.
Othetwise a positive respons to 4.64, no problems found.

The followin code will work, dispite the result returned from a com operation is an object
$aUnit is Vartype 9, but can be treated as an array using for each

$oCMS = CreateObject("CapaInstaller.SDK")
$RC = $oCMS.SetDefaultManagementPoint("2")
$aUnit = $oCMS.GetGroupPackages($Group,"Dynamic_SQL")
For Each $item In $aUnit ; This works despite $aUnut is an object type
WriteLog(Split($item,'|')[0])
next

The following code will fail:

$oCMS = CreateObject("CapaInstaller.SDK")
$RC = $oCMS.SetDefaultManagementPoint("2")
$aUnit = $oCMS.GetGroupPackages($Group,"Dynamic_SQL")
For $i = 0 To UBound($aUnit) ; This will not work BECAUSE $aUnut is an object type
WriteLog(Split($aUnit[$i],'|')[0])
next

I am glad that the for each statement works, but the same stament in vbscript has a method of typecating
to declare the returned object as an array, before retrieving it:

Set aUnit = CreateObject("System.Collections.ArrayList")
wscript.echo oCMS.SetDefaultManagementPoint("2")
Set aUnit = oCMS.GetGroupPackages("Test","Static")

Kind Regards
Erik Kærholm

Top
#207305 - 2013-05-14 06:40 AM Re: Treating an object as an Array [Re: kholm]
It_took_my_meds Offline
Hey THIS is FUN
*****

Registered: 2003-05-07
Posts: 273
Loc: Sydney, Australia
Hi Erik,

$aUnit is a collection not an array. The for each is not accessing the object by an index, rather it is calling a MoveNext method on the collection and assigning the result to $item at the end of each loop. I think the behaviour of KiXtart is correct and expected in this case.

Regards,

Richard

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 509 anonymous users online.
Newest Members
min_seow, Audio, Hoschi, Comet, rrosell
17881 Registered Users

Generated in 0.047 seconds in which 0.021 seconds were spent on a total of 13 queries. Zlib compression enabled.