PreviousDTO Programmer's Guide (9.1 revision 1) Next

Working with Collections

Show this topic in Library frames

Collections are objects that contain other objects.

Instantiating a Collection

Visual Basic

Use the Set keyword to set a variable to the collection object.

Dim result as DtoResult 
Dim my_session as New DtoSession 
Dim my_databases as DtoDatabases 
result = my_session.Connect("myserver", "username", 
"pw") 
if not(result=0) 
' error handling for Connect method 
end if 
' Use Set when an object or collection  
' is the return type 
Set my_databases = my_session.Databases 

ASP

When using ASP, you normally need to instantiate only two objects directly: DtoSession and DtoDictionary. Instantiating an object with Active Server Pages uses the Create Object syntax on the built-in Server object of IIS.

Set my_session = 
Server.CreateObject("DTO.DtoSession.2") 
 

Note
If you want the object to exist between HTTP calls, you must use the IIS built-in object Session to maintain the object's state, as shown in the following example.
Set Session("my_session") = Server.CreateObject("DTO.DtoSession.2")

However, if you create new objects, such as databases, DSNs, tables, columns, indexes, or segments, you use this same syntax. The progid for each object follows the convention shown in the preceding example.

For other collections, you can use the Set syntax documented previously for Visual Basic.

Looping through a Collection

Visual Basic

There are two ways to loop through collections in Visual Basic: using a For loop and using a counter.

The following shows the Visual Basic syntax to loop through a collection using a For/Next statement.

' obtain categories collection 
Dim my_categories as DtoCategories 
Dim category as DtoCategory 
Set my_categories = my_session.Categories 
' loop through collection 
For Each category In my_categories  
   settings = category.Settings 
Next 

The following shows the Visual Basic syntax to loop through a collection using a counter.

Dim column as DtoColumn 
Dim table as DtoTable 
Set table = dictionary.Tables("Billing") 
Dim i as long 
for i=1 to table.Columns.count 
  set column=table.Columns(i) 
  'perform operations with this column 
next i 

ASP

Here is sample ASP code that displays a list of categories for Pervasive configuration:

<% 
Set Session("my_session") = 
Server.CreateObject("DTO.DtoSession.2") 
result = Session("my_session").Connect("myserver", 
"username", "pw") 
' Error handling for Connect method not shown 
Set my_categories = Session("my_session").Categories 
' Now loop through and print categories in a unordered 
HTML list (<UL></UL>) 
%> 
<ul> 
<% For each category in my_categories %> 
<% ' The = (equal) sign displays the variable %> 
<% ' in the HTML stream and is a short cut  %> 
<% ' for the Response.Write() built-in  %> 
<% ' VBScript method. %> 
<li><%=cat.CategoryId> - <%=cat.Name%></li> 
<% Next %> 
</ul> 

Obtaining Number of Members

Visual Basic

Use the Count property to determine the number of objects in the collection.

Dim num_items as Integer 
Dim my_session as New DtoSession 
Dim result as DtoResult 
result = my_session.Connect("myserver", "username", 
"pw") 
' Error handling for Connect method not shown 
Set my_databases as my_session.Databases 
num_items = my_databases.Count 

ASP

Use the Count property to determine the number of objects in the collection.

<% 
Set Session("my_session") = 
Server.CreateObject("DTO.DtoSession.2") 
result = Session("my_session").Connect("myserver", 
"username", "pw") 
' Error handling for Connect method not shown 
Set my_databases = Session("my_session").Databases 
num_items = my_databases.Count 
' Now write output to HTML stream 
Response.Write("<p>Number of databases = " & num_items) 
%> 

Obtaining a Specific Member

Visual Basic and ASP

Use the Item property to obtain a specific member object of a collection.

Elements within collections can be accessed using their ordinals. Some collections support accessing elements using the element's unique property such as name. All ordinals are 1-based.

Since Item is a default property of the collection object, the following two statements are identical:

Collection.Item(index) 
Collection(index) 

Chapter contents
Publication contents

Prev topic: Object Summary
Next topic: Where to Find DTO Samples