Skip to content

cCollection Methods Reference ​

📋 Method List ​

MethodDescription
AddAdd element (auto-overwrite duplicate Key)
RemoveRemove element (by Key or Index)
RemoveAllClear all elements
UpdateUpdate existing element
ExistsCheck if Key exists
CountGet element count
KeyByIndexGet Key by Index
KeysGet all Key array
ItemsGet all Value array
RenameKeyRename a Key
SortByKeySort collection by Key
SortByValueSort collection by Value
GetSortedKeysGet sorted Key array (without modifying collection)
GetSortedValuesByKeyGet sorted Value array (without modifying collection)

➕ Add Method ​

Description ​

Adds an element to the collection. If a Key is provided and already exists, the old element is automatically removed before adding the new one (overwrite update).

Syntax ​

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

Parameters ​

ParameterTypeDescription
ItemVariantElement to add (can be any type)
KeyStringOptional, key name for the element

Example ​

vb
Dim col As New cCollection

' Add element without Key
col.Add "Plain element"

' Add string with Key
col.Add "John", "user1"

' Add number
col.Add 100, "count"

' Add object
Dim obj As New MyClass
col.Add obj, "myObject"

' Overwrite existing Key (auto remove old value)
col.Add "John-Updated", "user1"  ' Auto overwrite

➖ Remove Method ​

Description ​

Removes a specified element (by Key or Index).

Syntax ​

vb
Public Sub Remove(ByVal KeyOrIndex As Variant)

Parameters ​

ParameterTypeDescription
KeyOrIndexVariantKey (string) or Index (number, starts from 1)

Example ​

vb
' Remove by Key
col.Remove "user1"

' Remove by Index (remove first element)
col.Remove 1

🧹 RemoveAll Method ​

Description ​

Clears the collection, removing all elements.

Syntax ​

vb
Public Sub RemoveAll()

Example ​

vb
' Clear the collection
col.RemoveAll
Debug.Print col.Count  ' Output: 0

🔄 Update Method ​

Description ​

Updates an existing element. Returns False if Key does not exist.

Syntax ​

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

Parameters ​

ParameterTypeDescription
ItemVariantNew element value
KeyStringKey to update

Return Value ​

Boolean - Returns True on success, False if Key doesn't exist

Example ​

vb
' Update existing element
If col.Update("New value", "user1") Then
    Debug.Print "Update successful"
Else
    Debug.Print "Key not found"
End If

🔍 Exists Method ​

Description ​

Checks if the specified Key exists in the collection.

Syntax ​

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

Parameters ​

ParameterTypeDescription
KeyStringKey to check
CompareCaseBooleanOptional, case-sensitive comparison (default False)

Example ​

vb
' Default case-insensitive
col.Add "Value", "Key"
Debug.Print col.Exists("key")      ' True
Debug.Print col.Exists("KEY")      ' True

' Case-sensitive
col.Add "Value", "Key"
Debug.Print col.Exists("key", True)   ' False
Debug.Print col.Exists("Key", True)   ' True

🔢 Count Method ​

Description ​

Gets the number of elements in the collection.

Syntax ​

vb
Public Function Count() As Long

Example ​

vb
Debug.Print "Element count: " & col.Count

' Check if empty
If col.Count = 0 Then
    Debug.Print "Collection is empty"
End If

🔑 KeyByIndex Method ​

Description ​

Gets the Key corresponding to an index. Returns empty string if index is invalid or element has no Key.

Syntax ​

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

Parameters ​

ParameterTypeDescription
IndexLongIndex (starts from 1)

Example ​

vb
' Get Key of first element
Dim key As String
key = col.KeyByIndex(1)
If key <> "" Then
    Debug.Print "Key of first element: " & key
End If

📋 Keys Method ​

Description ​

Gets a string array containing all Keys. Elements without Key correspond to empty string.

Syntax ​

vb
Public Function Keys() As String()

Return Value ​

String() - Key array (0-based)

Example ​

vb
Dim keyArray() As String
keyArray = col.Keys()

Dim i As Long
For i = LBound(keyArray) To UBound(keyArray)
    Debug.Print "Key[" & i & "] = " & keyArray(i)
Next i

📦 Items Method ​

Description ​

Gets a Variant array containing all Values.

Syntax ​

vb
Public Function Items() As Variant()

Return Value ​

Variant() - Value array (0-based)

Example ​

vb
Dim itemArray() As Variant
itemArray = col.Items()

Dim i As Long
For i = LBound(itemArray) To UBound(itemArray)
    Debug.Print "Value[" & i & "] = " & itemArray(i)
Next i

✏️ RenameKey Method ​

Description ​

Renames a specified Key (Dictionary-compatible method).

Syntax ​

vb
Public Function RenameKey(ByVal OldKey As String, ByVal NewKey As String) As Boolean

Parameters ​

ParameterTypeDescription
OldKeyStringOriginal Key name
NewKeyStringNew Key name

Return Value ​

Boolean - Returns True on success, False on failure (original Key doesn't exist or new Key already exists)

Example ​

vb
' Rename Key
If col.RenameKey("oldName", "newName") Then
    Debug.Print "Rename successful"
Else
    Debug.Print "Rename failed (Key not found or new Key already exists)"
End If

🔤 SortByKey Method ​

Description ​

Sorts the entire collection in ascending alphabetical order by Key (reorganizes collection order).

Syntax ​

vb
Public Sub SortByKey()

Example ​

vb
' Add elements (unordered)
col.Add "Value-C", "Key-C"
col.Add "Value-A", "Key-A"
col.Add "Value-B", "Key-B"

' Sort by Key
col.SortByKey

' Now iteration will be in order: Key-A, Key-B, Key-C
Dim key As Variant
For Each key In col.Keys()
    Debug.Print key
Next

🔠 SortByValue Method ​

Description ​

Sorts the entire collection in ascending alphabetical order by Value (Value should be string type).

Syntax ​

vb
Public Sub SortByValue()

Example ​

vb
' Add elements
col.Add "Charlie", "C"
col.Add "Alpha", "A"
col.Add "Bravo", "B"

' Sort by Value
col.SortByValue

' Now order is: Alpha, Bravo, Charlie

📊 GetSortedKeys Method ​

Description ​

Gets a Key array sorted by Key (does not modify the original collection).

Syntax ​

vb
Public Function GetSortedKeys() As String()

Return Value ​

String() - Sorted Key array

Example ​

vb
' Get sorted Keys (without changing original collection order)
Dim sortedKeys() As String
sortedKeys = col.GetSortedKeys()

' Iterate using sorted Keys
Dim i As Long
For i = LBound(sortedKeys) To UBound(sortedKeys)
    Debug.Print sortedKeys(i) & " = " & col(sortedKeys(i))
Next i

📈 GetSortedValuesByKey Method ​

Description ​

Gets a Value array sorted by Key (does not modify the original collection).

Syntax ​

vb
Public Function GetSortedValuesByKey() As Variant()

Return Value ​

Variant() - Value array sorted by Key

Example ​

vb
' Get Values sorted by Key
Dim sortedValues() As Variant
sortedValues = col.GetSortedValuesByKey()

Dim i As Long
For i = LBound(sortedValues) To UBound(sortedValues)
    Debug.Print "Value[" & i & "] = " & sortedValues(i)
Next i

📌 Method Usage Scenarios Summary ​

Basic CRUD Operations ​

vb
Dim col As New cCollection

' Add
col.Add "Value", "Key"

' Check existence
If col.Exists("Key") Then
    ' Update
    col.Update "NewValue", "Key"
End If

' Delete
col.Remove "Key"

Batch Operations ​

vb
' Clear
col.RemoveAll

' Get all Keys and Values
Dim keys() As String
Dim values() As Variant
keys = col.Keys()
values = col.Items()

Sorting Scenarios ​

vb
' Need sorted iteration without modifying original collection
Dim sortedKeys() As String
sortedKeys = col.GetSortedKeys()

Dim i As Long
For i = LBound(sortedKeys) To UBound(sortedKeys)
    ProcessItem sortedKeys(i), col(sortedKeys(i))
Next i

' Directly sort collection (subsequent iterations are sorted)
col.SortByKey

Last Updated: 2026-05-17

VB6 and LOGO copyright of Microsoft Corporation