Any collection in ADOXCE can be enumerated (or iterated, as it is also known) to obtain a list of all the objects in the collection. For example, you may want to obtain a list of all the tables in a catalog. Each collection has a "Count" property that contains the number of items in the collection, and an Item function that returns an interface pointer to one of the items. In Listing 16.3, a TablesPtr tables collection interface is obtained from the _CatalogPtr interface using the GetTables method. The code then displays the number of tables and uses a "for" loop to get a _TablePtr interface for each of the tables in the collection. The Item function is passed the index of the table whose _TablePtr is to be obtained. This is passed as a VARIANT. Listing 16.3 Listing tables in a catalogvoid Listing16_3() { AdoXNS::_CatalogPtr pCatalog; AdoXNS::TablesPtr pTables; AdoXNS::_TablePtr pTable; if(!OpenCatalog(lpConnection, pCatalog)) return; // get collection of tables pTables = pCatalog->GetTables(); // List tables cout _T("Number of tables:") pTables->Count endl; for(short i = 0; i < pTables->Count; i++) { _variant_t vtIndex(i); pTable = pTables->Item[vtIndex]; cout _T("Table:") (LPTSTR)pTable->Name endl; } }
|