Skip to content

VBMAN.Collection - 增强集合对象

概述

VBMAN.Collection 提供了比 VB6 内置 Collection 更强大的功能,支持按键访问、存在性检查、Keys/Items 数组获取等,兼容 Scripting.Dictionary 的常用操作。

核心特性

  • 键值对存储: 支持使用 Key 访问元素
  • Dictionary 兼容: 兼容常用 Dictionary 操作
  • 存在性检查: 支持检查 Key 是否存在
  • 数组导出: 支持导出 Keys 和 Items 数组
  • 更新支持: 支持更新已存在的元素

属性

属性类型说明
Item(KeyOrIndex)Variant获取或设置元素(默认成员)
RawCollectionCollection原始 Collection 对象
CountLong元素数量(方法)

方法

Add

添加元素

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

参数:

  • Item - 要添加的元素
  • Key - 键名(可选,如果提供且已存在则替换)

示例:

vb
' 添加无键元素
VBMAN.Collection.Add "值1"

' 添加带键元素
VBMAN.Collection.Add "张三", "name"
VBMAN.Collection.Add 25, "age"

' 添加对象
VBMAN.Collection.Add New cJson, "config"

Remove

删除元素

vb
Public Sub Remove(ByVal KeyOrIndex As Variant)

示例:

vb
' 通过 Key 删除
VBMAN.Collection.Remove "name"

' 通过索引删除(从1开始)
VBMAN.Collection.Remove 1

RemoveAll

清空所有元素

vb
Public Sub RemoveAll()

示例:

vb
VBMAN.Collection.RemoveAll

Exists

检查 Key 是否存在

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

示例:

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

Update

更新已存在的元素

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

示例:

vb
If VBMAN.Collection.Update("李四", "name") Then
    Debug.Print "更新成功"
End If

Keys

返回所有 Key 的数组

vb
Public Function Keys() As String()

示例:

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

返回所有 Value 的数组

vb
Public Function Items() As Variant()

示例:

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

KeyByIndex

通过索引获取 Key

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

示例:

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

Count

返回元素数量

vb
Public Function Count() As Long

示例:

vb
Debug.Print "共有 " & VBMAN.Collection.Count & " 个元素"

综合示例

示例1: 基本操作

vb
Private Sub BasicOperations()
    ' 清空
    VBMAN.Collection.RemoveAll
    
    ' 添加数据
    VBMAN.Collection.Add "张三", "name"
    VBMAN.Collection.Add 25, "age"
    VBMAN.Collection.Add "北京", "city"
    
    ' 访问数据
    Debug.Print VBMAN.Collection("name")  ' 张三
    Debug.Print VBMAN.Collection("age")   ' 25
    
    ' 修改数据
    VBMAN.Collection("age") = 26
    
    ' 检查存在性
    If VBMAN.Collection.Exists("name") Then
        Debug.Print "name 存在"
    End If
    
    ' 删除数据
    VBMAN.Collection.Remove "city"
End Sub

示例2: 遍历集合

vb
Private Sub IterateCollection()
    ' 方式1: 通过索引
    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
    
    ' 方式2: 通过 Keys 数组
    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

示例3: 存储对象

vb
Private Sub StoreObjects()
    Dim json1 As New cJson
    json1("id") = 1
    json1("name") = "产品A"
    
    Dim json2 As New cJson
    json2("id") = 2
    json2("name") = "产品B"
    
    ' 存储对象
    VBMAN.Collection.Add json1, "product1"
    VBMAN.Collection.Add json2, "product2"
    
    ' 读取对象
    Dim product As cJson
    Set product = VBMAN.Collection("product1")
    Debug.Print product("name")
End Sub

最佳实践

  1. 键名规范: 使用有意义的键名,建议使用驼峰或下划线命名
  2. 存在性检查: 访问前使用 Exists 检查键是否存在
  3. 类型注意: VB6 是弱类型,注意存储和读取时的类型转换
  4. 对象释放: 存储对象时,注意对象生命周期管理

VB6及其LOGO版权为微软公司所有