突然有一个key-value的需求,本打算使用直接使用Colletion对象,无奈功能太单一,竟然无法更新成员,于是自己写了一个,感觉还不错,分享给大家。开展方法:Clear、Create、LoadXML(简化版,超实用)和ShowAllItem(用于跟踪调试比较方便,没有实际用途)完善或提意见有分!!以下为类文件内容,自己用记事本保存即可:
--------------------------------
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CollectionEx"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Option Compare TextDim Data() As Variant, vUbound As Long'通过 工具-过程属性-高级 选择 名称=Item,过程标识=缺省 设置为默认方法 调用 Class("Key")
Public Property Get Item(Key As String) As Variant
Attribute Item.VB_Description = "获取或设置集合成员的值"
Attribute Item.VB_UserMemId = 0
Dim i As Long
i = GetIndex(Key)
If i > -1 Then Item = Data(1, i)
End PropertyPublic Property Let Item(Key As String, ByVal vNewValue As Variant)
Dim i As Long
i = GetIndex(Key)
If i > -1 Then Data(1, i) = vNewValue
End PropertyPublic Property Get Count() As Long
Attribute Count.VB_Description = "返回集合的成员数"
Count = vUbound + 1
End PropertyPrivate Sub Class_Initialize()
vUbound = -1
End SubPrivate Sub Class_Terminate()
Erase Data()
End SubPublic Sub Add(Key As String, Value As Variant)
Attribute Add.VB_Description = "添加集合成员"
vUbound = vUbound + 1
ReDim Preserve Data(1, vUbound)
Data(0, vUbound) = CStr(Key)
Data(1, vUbound) = Value
End SubPublic Sub Clear()
Attribute Clear.VB_Description = "清除集合数据"
Erase Data()
vUbound = -1
End SubPublic Sub Create(ParamArray Keys() As Variant)
Attribute Create.VB_Description = "根据参数表重新创建集合的键"
Dim i As Long
Clear
vUbound = UBound(Keys)
ReDim Data(1, vUbound)
For i = 0 To vUbound
    Data(0, i) = CStr(Keys(i))
Next
End SubPublic Sub LoadXML(XML As String)
Attribute LoadXML.VB_Description = "以XML数据重新创建集合"
Dim i As Long
Clear
i = InStr(XML, "?>")
If i = 0 Then i = -1
GetFirstKey Mid(XML, i + 2)
End SubPublic Sub Remove(Key As String)
Attribute Remove.VB_Description = "删除指定集合成员"
Dim i As Long
i = GetIndex(Key)
If i > -1 Then
    Data(0, i) = Data(0, vUbound)
    Data(1, i) = Data(1, vUbound)
    vUbound = vUbound - 1
    ReDim Preserve Data(1, vUbound)
End If
End SubPublic Sub ShowAllItem()
Attribute ShowAllItem.VB_Description = "显示当前集合所有成员的键与值"
On Error Resume Next
Dim i As Long, stmp As String, v As String
For i = 0 To vUbound
    v = "": v = CStr(Data(1, i))
    stmp = stmp & Data(0, i) & "=" & v & vbCrLf
Next
MsgBox stmp, 64
End SubPrivate Function GetIndex(Key As String)
On Error Resume Next
Dim i As Long
GetIndex = -1
For i = 0 To vUbound
    If Data(0, i) = Key Then
        GetIndex = i
        Exit For
    End If
Next
End FunctionPrivate Function GetFirstKey(Src As String) As Boolean
On Error Resume Next
Dim i As Long, j As Long, c As String, Key As String, v As String
j = InStr(Src, "<") + 1
If j = 0 Then Exit Function
For i = j To Len(Src)
    c = Mid(Src, i, 1)
    If c = " " Or c = ">" Then
        If Key = "" Then Key = Mid(Src, j, i - j)
        If c = ">" Then
            j = InStr(Src, "</" & Key & ">")
            v = Mid(Src, i + 1, j - i - 1)
            GetFirstKey = True
            If v <> "" Then If Not GetFirstKey(v) Then Add Key, v
            v = Mid(Src, j + Len(Key) + 3)
            GetFirstKey v
            Exit For
        End If
    End If
Next
End Function

解决方案 »

  1.   

    微软脚本类中的字典类功能好像也很简单,上述扩展类实现的LoadXML还是很有用的对于一些简单xml开发还是很方便的
      

  2.   

    Dictionary 对象
                   描述对象,用于存储数据关键字和条目对。语法Scripting.Dictionary说明Dictionary 对象与 PERL 关联数组等价。可以是任何形式的数据的条目被存储在数组中。每个条目都与一个唯一的关键字相关联。该关键字用来检索单个条目,通常是整数或字符串,可以是除数组外的任何类型。下面的代码举例说明了如何创建一个 Dictionary 对象:Dim d                   '创建一个变量
    Set d = CreateObject(Scripting.Dictionary)
    d.Add "a", "Athens"     '添加一些关键字和条目
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"
    ...
      

  3.   

    Items 方法
             描述返回一个包含 Dictionary 对象中所有条目的数组。语法object.Itemsobject始终是一个 Dictionary 对象的名字。说明下面的代码举例说明了 Items 方法的使用。:Dim a, d, i             '创建一些变量
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "a", "Athens"     '添加一些关键字和条目。
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"
    a = d.Items             '取得条目
    For i = 0 To d.Count -1 '重复数组
        Print a(i)          '打印条目
    Next
    ...
      

  4.   

    我直接用Recordset对象。