Iterating Through a Collection

It is often useful to perform a series of operations on every specific object in a collection.

Visual Basic

Use the Visual Basic For Each statement to iterate through all specific objects in a collection. For example:

For Each oDatabase in oSQLServer.Databases
    Debug.Print oDatabase.Name
Next
C++

You can use the GetCount and GetItemByOrd functions of a collection object to iterate through all specific objects in a collection. Use the following syntax for GetCount:

pObjects->GetCount ( &lCount );

For example:

LPSQLOLEDATABASES pDatabases;
LPSQLOLEDATABASE pDatabase;
long i, lCount;
pSQLServer->GetDatabases (&pDatabases);
pDatabases->GetCount (&lCount);
for (i = 0; i < lCount; i+)
{
    pDatabases->GetItemByOrd (i, &pDatabase);
    // use the specific pDatabase object
}

You can also use the GetObjectCount and GetObjectByOrd "shortcut" functions of the parent object to iterate through all specific objects in a collection. For example:

LPSQLOLEDATABASE pDatabase;
long i, lCount;
pSQLServer->GetDatabaseCount (&lCount);
for (i = 0; i < lCount; i+)
{
    pSQLServer->GetDatabaseByOrd (i, &pDatabase);
    // use the specific pDatabase object
}