Enumerating collections

A collection is simply a set or group of items, where each can be accessed through its own Interface.  For example, Microsoft Word™ can have multiple documents open at the same time, and it can provide an Interface reference for each open document.

Therefore, enumerating a collection is simply a matter of determining the number of items in the collection, looping through and retrieving the appropriate information for one or more Interface members of the collection.

We'll start off with the Visual Basic syntax and show how to perform the same kind of task with PowerBASIC.

Visual Basic syntax for enumerating a collection looks something like this:

Dim Item As InterfaceItem

Dim Items As InterfaceItemsCollection

...

For Each Item In Items

  'do something with the Item.member Method/Property, e.g.,

  var$ = Item.StringProp

Next

In PowerBASIC, we can perform the same enumeration with just a little more code to handle the Variant data types.  For example:

DIM oItem  AS InterfaceItem

DIM oItems AS InterfaceItems

DIM vVnt  AS VARIANT

...

OBJECT GET oItems.Count TO vVnt

FOR Index& = 1 TO VARIANT#(vVnt)

  OBJECT GET oItems.Item(Index&) TO vVnt

  LET oItem = vVnt

  'do something with the Item.member Method/Property, e.g.,

  OBJECT GET oItem.StringProp TO vVnt

  var$ = VARIANT$(vVnt)

NEXT

 

See Also

COM Programming Introduction

The PowerBASIC COM Browser

Converting COM code to PowerBASIC

Example COM controller application

COM Programming Summary