VBMAN.Collection - Enhanced Collection Object
Overview
VBMAN.Collection provides more powerful functionality than VB6's built-in Collection, supporting key-based access, existence checks, Keys/Items array retrieval, etc., compatible with common Scripting.Dictionary operations.
Core Features
- Key-Value Pair Storage: Supports accessing elements using Key
- Dictionary Compatible: Compatible with common Dictionary operations
- Existence Check: Supports checking if a Key exists
- Array Export: Supports exporting Keys and Items arrays
- Update Support: Supports updating existing elements
Properties
| Property | Type | Description |
|---|---|---|
Item(KeyOrIndex) | Variant | Get or set element (default member) |
RawCollection | Collection | Raw Collection object |
Count | Long | Number of elements (method) |
Methods
Add
Add element
vb
Public Sub Add(ByVal Item As Variant, Optional ByVal Key As String = "")Parameters:
Item- Element to addKey- Key name (optional, if provided and already exists, it will be replaced)
Example:
vb
' Add element without key
VBMAN.Collection.Add "Value1"
' Add element with key
VBMAN.Collection.Add "Zhang San", "name"
VBMAN.Collection.Add 25, "age"
' Add object
VBMAN.Collection.Add New cJson, "config"Remove
Delete element
vb
Public Sub Remove(ByVal KeyOrIndex As Variant)Example:
vb
' Delete by Key
VBMAN.Collection.Remove "name"
' Delete by index (starts from 1)
VBMAN.Collection.Remove 1RemoveAll
Clear all elements
vb
Public Sub RemoveAll()Example:
vb
VBMAN.Collection.RemoveAllExists
Check if Key exists
vb
Public Function Exists(ByVal Key As String, Optional CompareCase As Boolean) As BooleanExample:
vb
If VBMAN.Collection.Exists("name") Then
Debug.Print VBMAN.Collection("name")
End IfUpdate
Update existing element
vb
Public Function Update(ByVal Item As Variant, ByVal Key As String) As BooleanExample:
vb
If VBMAN.ToolsDic.Update("Li Si", "name") Then
Debug.Print "Update successful"
End IfKeys
Return array of all Keys
vb
Public Function Keys() As String()Example:
vb
Dim keyArray() As String
keyArray = VBMAN.Collection.Keys
Dim i As Long
For i = LBound(keyArray) To UBound(keyArray)
Debug.Print keyArray(i)
Next iItems
Return array of all Values
vb
Public Function Items() As Variant()Example:
vb
Dim itemArray() As Variant
itemArray = VBMAN.Collection.ItemsKeyByIndex
Get Key by index
vb
Public Function KeyByIndex(ByVal Index As Long) As StringExample:
vb
Dim key As String
key = VBMAN.Collection.KeyByIndex(1)Count
Return number of elements
vb
Public Function Count() As LongExample:
vb
Debug.Print "Total " & VBMAN.Collection.Count & " elements"Comprehensive Examples
Example 1: Basic Operations
vb
Private Sub BasicOperations()
' Clear
VBMAN.Collection.RemoveAll
' Add data
VBMAN.Collection.Add "Zhang San", "name"
VBMAN.Collection.Add 25, "age"
VBMAN.Collection.Add "Beijing", "city"
' Access data
Debug.Print VBMAN.Collection("name") ' Zhang San
Debug.Print VBMAN.Collection("age") ' 25
' Modify data
VBMAN.Collection("age") = 26
' Check existence
If VBMAN.Collection.Exists("name") Then
Debug.Print "name exists"
End If
' Delete data
VBMAN.Collection.Remove "city"
End SubExample 2: Iterate Collection
vb
Private Sub IterateCollection()
' Method 1: By index
Dim i As Long
For i = 1 To VBMAN.Collection.Count
Dim key As String
key = VBMAN.Collection.KeyByIndex(i)
If key <> "" Then
Debug.Print key & " = " & VBMAN.Collection(key)
End If
Next i
' Method 2: By Keys array
Dim keys() As String
keys = VBMAN.Collection.Keys
Dim k As Variant
For Each k In keys
Debug.Print k & " = " & VBMAN.Collection(CStr(k))
Next k
End SubExample 3: Store Objects
vb
Private Sub StoreObjects()
Dim json1 As New cJson
json1("id") = 1
json1("name") = "Product A"
Dim json2 As New cJson
json2("id") = 2
json2("name") = "Product B"
' Store objects
VBMAN.Collection.Add json1, "product1"
VBMAN.Collection.Add json2, "product2"
' Read objects
Dim product As cJson
Set product = VBMAN.Collection("product1")
Debug.Print product("name")
End SubBest Practices
- Key Naming Convention: Use meaningful key names, recommend camelCase or snake_case naming
- Existence Check: Use Exists to check if key exists before accessing
- Type Awareness: VB6 is weakly typed, pay attention to type conversion when storing and reading
- Object Release: When storing objects, pay attention to object lifecycle management