主题:[原创]【原创】List类
孙瑞
[专家分:590] 发布于 2014-04-22 13:50:00
以下代码是.cls文件的代码,实现一个简单的List类,可以方便的添加删除项目
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "List"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'实现List类型的功能
'提供如下功能
'Add(String,Object)
'Get:Count
'Get:Item
Private ItemCount As Long '计数一共有多少条数据
Private FullSize As Long '计数当前一共有多大的空间被占用
Const IncreaseSize As Long = 50 '设置增加一次的值
Public CurrentIndex As Long
Private ProName() As String
Private ProIndex() As Integer
Private ProState() As Boolean
Private ProValue() As String
Public Property Get Count() As Long
Count = ItemCount
End Property
Private Sub Class_Initialize()
CurrentIndex = 0
FullSize = IncreaseSize
ReDim ProName(IncreaseSize)
ReDim ProIndex(IncreaseSize)
ReDim ProState(IncreaseSize)
ReDim ProValue(IncreaseSize)
End Sub
Private Sub Resize()
Dim i As Integer
FullSize = FullSize + IncreaseSize
ReDim Preserve ProName(FullSize)
ReDim Preserve ProIndex(FullSize)
ReDim Preserve ProState(FullSize)
ReDim Preserve ProValue(FullSize)
'对扩大后的面积进行初始化
For i = FullSize To FullSize - IncreaseSize + 1 Step -1
ProName(FullSize) = ""
ProState(FullSize) = False
ProIndex(FullSize) = 0
ProValue(FullSize) = ""
Next
End Sub
回复列表 (共1个回复)
沙发
孙瑞 [专家分:590] 发布于 2014-04-22 13:54:00
Public Function listFull() As Boolean
Dim i As Long
For i = 1 To FullSize
If ProState(i) = False Then
listFull = False
Exit Function
End If
Next i
listFull = True
End Function
Public Sub Add(ByVal Key As String, ByVal value As String)
If listFull = True Then
Resize
End If
CurrentIndex = GetLastIndex(Key)
ProName(CurrentIndex) = Key
ProValue(CurrentIndex) = value
ProState(CurrentIndex) = True
ProIndex(CurrentIndex) = CurrentIndex
End Sub
Public Sub RemoveByID(ByVal index As Long)
ProState(index) = False
ItemCount = ItemCount - 1
End Sub
Public Sub RemoveByName(ByVal Key As String)
Dim index As Long
index = ItemID(Key)
If index = -1 Then
Exit Sub
Else
RemoveByID (index)
End If
End Sub
Public Function Item(ByVal index As Long) As String
If index > 0 And index <= FullSize Then
If ProState(index) = True Then
Item = ProValue(index)
Exit Function
End If
End If
Item = ""
End Function
Public Function ItemByName(ByVal ItemName As String) As String
Dim i As Long
i = ItemID(ItemName)
If i = -1 Then
ItemByName = ""
Else
ItemByName = ProValue(i)
End If
End Function
Private Function ItemID(ByVal ItemName As String) As Long
Dim i As Long
For i = 1 To FullSize
If ProName(i) = ItemName Then
ItemID = i
Exit Function
End If
Next i
ItemID = -1
End Function
Public Function GetLastIndex(ByVal Key As String) As Long
Dim index As Long
index = ItemID(Key)
If index <= 0 Then
GetLastIndex = 0
Exit Function
End If
If ProState(index) = False Then
ItemCount = ItemCount + 1
End If
GetLastIndex = index
End Function
我来回复