cCollection Properties Reference
📋 Property List
| Property | Type | Read/Write | Description |
|---|---|---|---|
Item | Variant | Read/Write | Get or set element (default property) |
RawCollection | Collection | Read-only | Get underlying Collection object |
📦 Item Property (Default Property)
Description
Gets or sets an element in the collection. This is the default property, allowing direct use of col("key") syntax.
Supports access via Key (string) or Index (number, starting from 1).
Syntax
vb
' Get
Property Get Item(ByVal KeyOrIndex As Variant) As Variant
' Let (value type)
Property Let Item(ByVal KeyOrIndex As Variant, ByVal Value As Variant)
' Set (object type)
Property Set Item(ByVal KeyOrIndex As Variant, ByVal Value As Object)Parameters
| Parameter | Type | Description |
|---|---|---|
KeyOrIndex | Variant | Key (string) or Index (number, starts from 1) |
Value | Variant/Object | Value to set |
Example
Getting Elements
vb
Dim col As New cCollection
col.Add "John", "user1"
col.Add "Jane", "user2"
' Get by Key (explicit syntax)
Dim name As String
name = col.Item("user1")
' Short form (using default property)
name = col("user1")
' Get by Index (starts from 1)
name = col(1) ' Get first elementSetting Elements (Value Type)
vb
' Update by Key (adds if Key doesn't exist)
col.Item("user1") = "John-Updated"
' Short form
col("user1") = "John-Updated"
' Update by Index (requires element at that position)
col(1) = "New value"Setting Elements (Object Type)
vb
Dim col As New cCollection
' Add object
col.Add CreateObject("Scripting.Dictionary"), "dict1"
' Update object using Set
Dim newDict As Object
Set newDict = CreateObject("Scripting.Dictionary")
newDict.Add "key", "value"
Set col("dict1") = newDictIterating Through Collection
vb
' For Each iteration (using NewEnum)
Dim item As Variant
For Each item In col
Debug.Print item
Next
' Iterate by index
Dim i As Long
For i = 1 To col.Count
Debug.Print col(i)
Next
' Iterate by Keys
Dim keys() As String
keys = col.Keys()
Dim key As Variant
For Each key In keys
Debug.Print key & " = " & col(CStr(key))
Next⚠️ Notes
- Index starts from 1: Consistent with VB Collection, index starts from 1
- Key not found: Getting returns Empty, setting adds new element
- Object handling: Must use
Setkeyword when getting objects
📦 RawCollection Property
Description
Gets the underlying raw Collection object. Used for advanced scenarios requiring direct Collection manipulation.
Syntax
vb
Property Get RawCollection() As CollectionReturn Value
Collection - The underlying VB Collection object
Example
vb
' Get underlying Collection
Dim rawCol As Collection
Set rawCol = col.RawCollection
' Directly use Collection methods
' Note: Direct manipulation of rawCol won't sync m_Keys, use with caution
Dim count As Long
count = rawCol.Count⚠️ Notes
Directly manipulating RawCollection may cause Keys collection to become out of sync. It is recommended to use this only for read operations.
📌 Property Usage Scenarios Summary
Common Access Patterns
vb
Dim col As New cCollection
col.Add "John", "user1"
' Recommended: Access by Key
Debug.Print col("user1")
' Access by Index (when position is known)
Debug.Print col(1)
' Safe access (check existence first)
If col.Exists("user1") Then
Debug.Print col("user1")
End IfObject Handling
vb
' Store object
col.Add SomeObject, "obj1"
' Read object (must use Set)
Dim obj As MyClass
Set obj = col("obj1")
' Update object (must use Set)
Set col("obj1") = NewObjectLast Updated: 2026-05-17