Home > Software engineering >  Sharing a set extension class (source)
Sharing a set extension class (source)

Time:09-22

Suddenly there was a key - value demand, intended to use directly using the Colletion object, but function is too single, unexpectedly cannot update members, so he had to write a, feel is good, share with you,

Method: the Clear, Create, LoadXML (simplified version, super practical) and ShowAllItem (for tracking debugging more convenient, there is no actual use)

Perfect or advice!!

The following content for the class files, to save with notepad:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
VERSION 1.0 CLASS
The BEGIN
MultiUse=1 'True
Persistable=0 'NotPersistable
DataBindingBehavior=0 'vbNone
DataSourceBehavior=0 'vbNone
MTSTransactionMode=0 'NotAnMTSObject
END
The Attribute VB_Name="CollectionEx"
The Attribute VB_GlobalNameSpace=False
The Attribute VB_Creatable=True
The Attribute VB_PredeclaredId=False
The Attribute VB_Exposed=False
Option Explicit
Option Compare Text

Dim the Data () As the Variant, vUbound As Long

'through the process of tool - property - senior name=Item choice, process id=default setting is the default method calls the Class (" Key ")
Public Property Get Item (Key As String) As the Variant
Attribute Item. VB_Description="get or set the value of collection member"
The Attribute Item. VB_UserMemId=0
Dim As Long I
I=GetIndex (Key)
If I & gt; 1 Then the Item=Data (1, I)
End Property

Public Property Let Item (Key As String, ByVal vNewValue As the Variant)
Dim As Long I
I=GetIndex (Key)
If I & gt; 1 Then the Data (1, I)=vNewValue
End Property

Public Property Get the Count () As Long
Attribute Count. VB_Description="return to set the number of members"
Count=vUbound + 1
End Property

Private Sub Class_Initialize ()
VUbound=1
End Sub

Private Sub Class_Terminate ()
Erase the Data ()
End Sub

Public Sub Add (Key As String, the Value As the Variant)
Attribute to the Add. VB_Description="Add collection member"
VUbound=vUbound + 1
ReDim Preserve Data (1, vUbound)
Data (0, vUbound)=CStr (Key)
Data (1, vUbound)=Value
End Sub

Public Sub the Clear ()
Attribute Clear. VB_Description="removal of collection data"
Erase the Data ()
VUbound=1
End Sub

Public Sub Create (ParamArray Keys () As the Variant)
Attribute the Create. VB_Description="according to the parameter table to Create a collection of key"
Dim As Long I
The 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 a String)
Attribute LoadXML. VB_Description="in XML data to create the collection"
Dim As Long I
The Clear
I=InStr (XML, "? & gt;" )
If I=0 Then I=1
GetFirstKey mids (XML, I + 2)
End Sub

Public Sub Remove (Key As String)
Attribute. Remove VB_Description="delete members of the specified collection
"Dim As Long I
I=GetIndex (Key)
If I & gt; 1 Then
Data (0, I)=Data (0, vUbound)
Data (1, I)=Data (1, vUbound)
VUbound=vUbound - 1
ReDim Preserve Data (1, vUbound)
End the If
End Sub

Public Sub ShowAllItem ()
The Attribute ShowAllItem. VB_Description="displays all the members of the current set of keys and values"
On the Error Resume Next
STMP As String, v Dim I As Long As the String
For I=0 To vUbound
V="" : v=CStr (Data (1, I))
STMP=STMP & amp; Data (0, I) & amp; "=" & amp; V & amp; VbCrLf
Next
MsgBox STMP, 64
End Sub

Private Function GetIndex (Key As String)
On the Error Resume Next
Dim As Long I
GetIndex=1
For I=0 To vUbound
If the Data (0, I)=Key Then
GetIndex=I
The Exit For
End the If
Next
End the Function

Private Function GetFirstKey (Src As String) As Boolean
On the Error Resume Next
Dim As Long I, j, As Long As String, c Key As String, v As String
J=InStr (Src, & lt; "" ) + 1
If j=0 Then the Exit Function
For I=j To Len (Src)
C=Mid (Src, I, 1)
If c=="Or" c "& gt;" Then
If the Key="" Then Key=Mid (Src, j, I - j)
If c="& gt;" Then
J=InStr (Src, "& lt;/"& amp; The Key & amp; "& gt;" )
V=Mid (Src, I + 1, j - I - 1)
GetFirstKey=True
If v & lt;> "" Then If Not GetFirstKey (v) Then Add Key, v
V=Mid (Src, j + Len (Key) + 3)
GetFirstKey v
The Exit For
End the If
End the If
Next
End the Function

CodePudding user response:

Why not use dictionary?

CodePudding user response:

Microsoft script of the dictionary class functions as if also is very simple, the extended class implements the LoadXML is very useful for the development of some simple XML is very convenient

CodePudding user response:

CodePudding user response:

The dictionary, a dictionary, a dictionary... The dictionary, the good stuff

CodePudding user response:

 Dictionary object 


Description

Keywords and object, used to store data entry to,

Syntax

Scripting. The Dictionary

Description

Dictionary object with a PERL associative array equivalent, can be any form of data entries are stored in arrays, each entry is associated with a unique key, the key used to retrieve a single item, is usually an integer or a string, any type, may be a divisor group

The following code illustrates how to create a Dictionary object:

Dim d 'create a variable
The Set d=CreateObject (Scripting. The Dictionary)
Da dd "a", "native" 'add some keywords and entry
Belgrade da dd "b", "
Da dd "c", "Cairo
".
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related