cCollection 概述
📖 简介
cCollection 是一个增强版的 VB Collection 类,在标准 Collection 基础上增加了 Dictionary 兼容的方法,以及排序、键管理等功能。
✨ 主要特性
| 特性 | 说明 |
|---|---|
| 字典兼容 | 提供 Keys/Items/RenameKey 等 Dictionary 风格方法 |
| 键管理 | 支持通过 Key 或 Index 访问、修改元素 |
| 排序功能 | 支持按 Key 或 Value 对集合进行排序 |
| 安全更新 | Add 方法自动处理重复 Key(覆盖更新) |
| For Each 支持 | 支持 VB 标准遍历语法 |
🚀 快速开始
创建集合并添加元素
vb
Dim col As New cCollection
' 添加带 Key 的元素
col.Add "张三", "user1"
col.Add "李四", "user2"
' 添加不带 Key 的元素
col.Add "普通元素"访问元素
vb
' 通过 Key 访问
Dim name As String
name = col.Item("user1")
' 通过 Index 访问(从1开始)
name = col.Item(1)
' 简写形式(默认属性)
name = col("user1")遍历集合
vb
' For Each 遍历
Dim item As Variant
For Each item In col
Debug.Print item
Next
' 通过索引遍历
Dim i As Long
For i = 1 To col.Count
Debug.Print col(i)
Next检查键是否存在
vb
If col.Exists("user1") Then
Debug.Print "用户已存在"
End If
' 不区分大小写(默认)
If col.Exists("USER1") Then ' 返回 True📁 文档导航
| 文档 | 说明 |
|---|---|
| methods.md | 方法详细参考(Add、Remove、SortByKey 等) |
| properties.md | 属性详细参考(Count、Item 等) |
🆚 与标准 Collection/Dictionary 对比
| 功能 | Collection | Dictionary | cCollection |
|---|---|---|---|
| Key 访问 | ✓ | ✓ | ✓ |
| Index 访问 | ✓ | - | ✓ |
| Exists 检查 | - | ✓ | ✓ |
| Keys/Items 方法 | - | ✓ | ✓ |
| 排序功能 | - | - | ✓ |
| 重复 Key 处理 | 报错 | 覆盖 | 覆盖 |
| For Each 支持 | ✓ | ✓ | ✓ |
最后更新: 2026-05-17