Skip to content

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 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 ​

PropertyTypeDescription
Item(KeyOrIndex)VariantGet or set element (default member)
RawCollectionCollectionRaw Collection object
CountLongNumber of elements (method)

Methods ​

Add ​

Add element

vb
Public Sub Add(ByVal Item As Variant, Optional ByVal Key As String = "")

Parameters:

  • Item - Element to add
  • Key - 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 1

RemoveAll ​

Clear all elements

vb
Public Sub RemoveAll()

Example:

vb
VBMAN.Collection.RemoveAll

Exists ​

Check if Key exists

vb
Public Function Exists(ByVal Key As String, Optional CompareCase As Boolean) As Boolean

Example:

vb
If VBMAN.Collection.Exists("name") Then
    Debug.Print VBMAN.Collection("name")
End If

Update ​

Update existing element

vb
Public Function Update(ByVal Item As Variant, ByVal Key As String) As Boolean

Example:

vb
If VBMAN.ToolsDic.Update("Li Si", "name") Then
    Debug.Print "Update successful"
End If

Keys ​

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 i

Items ​

Return array of all Values

vb
Public Function Items() As Variant()

Example:

vb
Dim itemArray() As Variant
itemArray = VBMAN.Collection.Items

KeyByIndex ​

Get Key by index

vb
Public Function KeyByIndex(ByVal Index As Long) As String

Example:

vb
Dim key As String
key = VBMAN.Collection.KeyByIndex(1)

Count ​

Return number of elements

vb
Public Function Count() As Long

Example:

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 Sub

Example 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 Sub

Example 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 Sub

Best Practices ​

  1. Key Naming Convention: Use meaningful key names, recommend camelCase or snake_case naming
  2. Existence Check: Use Exists to check if key exists before accessing
  3. Type Awareness: VB6 is weakly typed, pay attention to type conversion when storing and reading
  4. Object Release: When storing objects, pay attention to object lifecycle management

VB6 and LOGO copyright of Microsoft Corporation