Table of Contents

6.1.3.1 Collection Object

Note

This section describes the implementation of MS-VBAL §6.1.3.1 Collection Object.

The Collection class is represented in the SDK by the interface IStdCollectionClass.

A For Each statement finds an object's enumeration member through VBReturningMemberSymbol / SymbolProperties.UserMemId / WellKnownDispIds.NewEnum. This is the same lookup the constructor of VBCollectionType uses (RD-VBAL §5.4.2.4 For Each Statement).

Instances

A collection is one of many: each New Collection is an object of its own, with the members it was given. The implementation is one object for the whole class, and the standard-library dispatcher tells it which instance a call is on (IStdLibReceiverBound) for as long as that call lasts; what is in a collection is kept with the instance, so it lasts exactly as long as the object does.

Item is the default member of the class (VB_UserMemId = 0), so c(1) is c.Item(1): an index expression whose callee is an object is a call of its class's default member (RD-VBAL §5.6.13 Index Expressions), a class of the workspace's or the library's alike.

An object is a member as itself: it is passed to a Variant parameter by Set-assignment, and the parameter holds the object, not what the object's default member returns (RD-VBAL §5.3.1.11).

Note

Not implemented. A collection does not count the objects it holds. An object that only a collection holds is not kept alive by it, and one that was held by it is not let go of when it is removed; the same gap ObjectReferences documents for an array element and a field.

Keys, Positions and Errors

A key is a String, and is the same key whatever the case of its letters. A member is reached by its position (from 1 to Count) or by its key.

Situation Runtime error
A position that is not in the collection (Item, Remove, Before, After). 9, Subscript out of range.
A key that no member has. 5, Invalid procedure call or argument.
A key that a member already has (Add). 457, This key is already associated with an element of this collection. Nothing is added.
Both Before and After given (Add). 5.
A key that is not a String, an index that is neither a number nor a String, a user-defined type as a member. 13, Type mismatch.

Enumeration

The collection's enumeration member is the hidden _NewEnum (VB_UserMemId = -4), which VBA source reaches as [_NewEnum]. It returns an object of the hidden IEnumVARIANT class (IStdEnumVariantClass): a position in a snapshot of the members the collection had when it was asked, which MoveNext, Current and Reset move and read. A member added or removed afterwards is not that enumerator's.

MS-VBAL leaves the enumeration of an object "implementation-defined"; in MS-VBA the enumerator is a COM IEnumVARIANT, whose Next fills an array that VBA source cannot call. This is the same enumerator in a shape source can call, and is what a class of the workspace answers its own _NewEnum with, as mItems.[_NewEnum].

6.1.3.1.1 Public Functions

Note

This section describes the implementation of MS-VBAL §6.1.3.1.1 Public Functions.

§ Member Notes
6.1.3.1.1.1 Count
6.1.3.1.1.2 Item

6.1.3.1.2 Public Subroutines

Note

This section describes the implementation of MS-VBAL §6.1.3.1.2 Public Subroutines.

§ Member Notes
6.1.3.1.2.1 Add
6.1.3.1.2.2 Remove

⏮️ RD-VBAL §6.1.3 Predefined Class Modules | ⏭️ RD-VBAL §6.1.3.2 Err Class