突然有一個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 Text
Dim 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 Property
Public 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 Property
Public Property Get Count() As Long
Attribute Count.VB_Description = "回傳集合的成員數"
Count = vUbound + 1
End Property
Private Sub Class_Initialize()
vUbound = -1
End Sub
Private Sub Class_Terminate()
Erase Data()
End Sub
Public 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 Sub
Public Sub Clear()
Attribute Clear.VB_Description = "清除集合資料"
Erase Data()
vUbound = -1
End Sub
Public 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 Sub
Public 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 Sub
Public 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 Sub
Public 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 Sub
Private 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 Function
Private 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
uj5u.com熱心網友回復:
為什么不用字典類呢?uj5u.com熱心網友回復:
微軟腳本類中的字典類功能好像也很簡單,上述擴展類實作的LoadXML還是很有用的對于一些簡單xml開發還是很方便的uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
字典,字典,字典...........,字典,好東西喲uj5u.com熱心網友回復:
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"
...
uj5u.com熱心網友回復:
Items 方法
描述
回傳一個包含 Dictionary 物件中所有條目的陣列。
語法
object.Items
object始終是一個 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
...
uj5u.com熱心網友回復:
我直接用Recordset物件。uj5u.com熱心網友回復:
+1
這功能很少有人用,因為大部分人都以為他只能裝資料庫查詢的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/65442.html
標籤:VB基礎類
上一篇:label控制元件置頂問題
