.Net 和 Java 中都有 ArrayList 类,非常方便。VBS 中却没有,给使用带来了不便。所以特意编写了 VBS 的 ArrayList 类,功能与.Net 和 Java 中的 ArrayList 类相似,没有排序功能。
<%
'**********************************************************************************
' ArrayList 类
'**********************************************************************************
Class CArrayList
Private Items()
Private ItemUBound
Private ItemCount
'=================================================
'类型:私有方法
'作用:类初始化
'参数:无
'返回值:无
'说明:在类实例New时自动调用
'=================================================
Private Sub Class_Initialize
ItemUBound = -1
ItemCount = 15
ReDim Items(ItemCount)
End Sub
'=================================================
'=================================================
'类型:私有方法
'作用:类终止
'参数:无
'返回值:无
'说明:在销毁类实例时自动调用
'=================================================
Private Sub Class_Terminate()
ReDim Items(-1)
End Sub
'=================================================
'=================================================
'类型:方法
'作用:向ArrayList中添加一个元素
'参数:添加的对象。
'返回值:返回该元素添加到ArrayList中的位置。
'说明:VBS中定义对对象的赋值必须用SET,而其它类型的
' 赋值却不用SET,直接赋值即可。该方法自动判断对象
' 类型。
'=================================================
Public Function Add(Value)
Select Case TypeName(Value)
Case "Empty", "Null", "Unknown", "Nothing", "Error"
Add = -1
Exit Function
End Select
ItemUBound = ItemUBound + 1
If ItemUBound > (ItemCount-1) Then
ItemCount = ItemCount + 15
ReDim Preserve Items(ItemCount)
End If
Select Case TypeName(Value)
Case "Byte", "Integer" , "Long" , "Single" , "Double" , "Currency" , "Decimal" , "Date" , "String" , "Boolean"
Items(ItemUBound) = Value
Case Else
Set Items(ItemUBound) = Value
End Select
Add = ItemUBound
End Function
'=================================================
'=================================================
'类型:方法
'作用:向ArrayList中添加一个数组。
'参数:元素数组。
'返回值:无
'=================================================
Public Sub AddArray(Value)
Dim arrCount, I
If Not IsArray(Value) Then Exit Sub
arrCount = UBound(Value)
If arrCount < 0 Then Exit Sub
arrCount = arrCount + 1
ItemUBound = ItemUBound + arrCount
ReDim Preserve Items(ItemUBound)
For I = (ItemUBound - arrCount + 1) To ItemUBound - 1
Items(I) = Value(I + 1)
Next
End Sub
'=================================================
'=================================================
'类型:方法
'作用:向ArrayList中添加另一个ArrayList对象中的所有元素
'参数:ArrayList对象
'返回值:无
'=================================================
Public Sub AddArrayList(Value)
If TypeName(Value)<>"CArrayList" Then Exit Sub
AddArray(Value.GetArray)
End Sub
'=================================================
'=================================================
'类型:方法
'作用:从ArrayList中移除指定索引号的元素
'参数:要移除元素的索引号
'返回值:无
'=================================================
Public Sub Remove(Index)
If Index >=0 And Index <= ItemUBound Then
Dim I
For I = Index To ItemUBound - 1
Items(I) = Items(I+1)
Next
ItemUBound = ItemUBound - 1
'ReDim Preserve Items(ItemUBound)
End If
End Sub
'=================================================
'=================================================
'类型:公共方法
'作用:返回指定元素的索引号
'参数:元素
'返回值:
'说明:
'=================================================
Public Function IndexOf(Value)
Dim I,R
R = -1
For I = 0 To ItemUBound
If Items(Index) = Value Then
R = I
Exit Function
End If
Next
IndexOf = R
End Function
'=================================================
'类型:方法
'作用:返回指定索引号的元素
'参数:要返回的元素的索引号
'返回值:指定的元素对象
'说明:
'=================================================
Public Function Item(Index)
If Index>=0 And Index <= ItemUBound Then
Select Case TypeName(Items(Index))
Case "Byte", "Integer" , "Long" , "Single" , "Double" , "Currency" , "Decimal" , "Date" , "String" , "Boolean"
Item = Items(Index)
Case Else
Set Item = Items(Index)
End Select
End If
End Function
'=================================================
'=================================================
'类型:方法
'作用:清除ArrayList所有元素
'参数:无
'返回值:无
'说明:
'=================================================
Public Sub Clear
Class_Initialize
End Sub
'=================================================
'=================================================
'类型:属性(只读)
'作用:返回元素总数
'=================================================
Public Property Get Count
Count = ItemUBound + 1
End Property
'=================================================
'=================================================
'类型:方法
'作用:返回包含ArrayList中所有元素的数组
'参数:无
'返回值:包含ArrayList中所有元素的数组
'=================================================
Public Function GetArray
Dim RItem(), I
ReDim RItem(ItemUBound)
For I = 0 To ItemUBound
RItem(I) = Items(I)
Next
GetArray = RItem
End Function
'=================================================
End Class
'**********************************************************************************
%>