Accessing Collections of Objects
Go Up to Automation Objects Programmer's Guide
Collections are groups of Objects of the same type. For example, the Models object is a collection of all the Models in a Diagram.
Each Collection has four methods:
- Item (), this is the default method
- Add()
- Remove()
- Count.()
You can access objects by name (and sequence number or technical key, when appropriate). The following code sample illustrates default behavior:
Dim myentity as Entity
Set myentity = mymodel.Entities("Entity1")
In this example, the Item() method in the Entities collection is actually called to return the Entity1 object, but the script does not need to contain an explicit reference to the method because it is the default method.
The Add(), Remove(), and Count() methods are supported for all collections, except where noted. These methods modify the internal data in ER/Studio Data Architect immediately. The Add() method adds a new object into the collection. Add() fails if the object is not properly initialized. The Count() returns the number of objects in the collection.
Iterating Through Collections
The most-often used way of retrieving each object in a collection is with the For Each... Next loop. Note the use of the variable HowMany to start and iterate the loop below.
' Sets the entity name for each entity in the given collection
Dim MyDiagram As Diagram
Dim MyModel As Model
Dim MyEntity As Entity
Dim EntName As String
Dim HowMany As Integer
Set MyDiagram = DiagramManager.ActiveDiagram
Set MyModel = MyDiagram.ActiveModel
HowMany = 1
' Iterates through the Entities collection to rename all entities
For Each MyEntity In MyModel.Entities
' Uses CStr function to convert the Count variable to a string
EntName = "ShinyNewEntity" + CStr (HowMany)
MyEntity.EntityName = EntName
HowMany = HowMany + 1
Next MyEntity