asp 动态数组 提供Add、Insert、Remove、RemoveAt、Search等方法。

来源:asp之家 时间:2011-03-17 11:10:00 

代码如下:


Class Vector 

Private vector_datas() 
Private initial_capacity '初始化容量 
Private capacity_increment '容量增量 
Private element_count '元素数 
Private max_capacity '总容量 

Private Sub Class_Initialize() 
RemoveAll 
End Sub 

Public Function RemoveAll() 
element_count = 0 
initial_capacity = 10 
capacity_increment = 10 
max_capacity = initial_capacity 
ReDim vector_datas(initial_capacity) 
End Function 

Public Property Get Count() 
Count = element_count 
End Property 

Public Property Get Capacity() 
Capacity = max_capacity 
End Property 

Public Property Get InitialCapacity() 
InitialCapacity = initial_capacity 
End Property 

Public Property Get CapacityIncrement() 
CapacityIncrement = capacity_increment 
End Property 

Public Default Property Get Item(index) 
If IsObject(vector_datas(index)) Then 
Set Item = vector_datas(index) 
Else 
Item = vector_datas(index) 
End If 
End Property 

Public Function Add(element) 
Call Insert(element_count, element) 
End Function 

Public Function Remove(element) 
Dim index 
index = Search(element) 
RemoveAt(index) 
Remove = index 
End Function 

Public Function RemoveAt(index) 
Dim i 
For i = index + 1 To element_count - 1 Step 1 
Call InternalElement(i - 1, vector_datas(i)) 
Next 
element_count = element_count - 1 
If max_capacity - capacity_increment > element_count Then 
max_capacity = max_capacity - capacity_increment 
ReDim Preserve vector_datas(max_capacity) 
End If 
End Function 

Public Function Search(element) 
Dim i 
For i = 0 To element_count - 1 Step 1 
If vector_datas(i) = element Then 
Search = i 
Exit Function 
End If 
Next 
Search = -1 
End Function 

Public Function Insert(index, element) 
If index > element_count Then 
Err.Raise 20903, "Vector", "Array Index Out Of Bounds.", "", 0 
End If 
If element_count = 0 Then 
Call InternalElement(0, element) 
ElseIf index = element_count Then 
Call InternalElement(element_count, element) 
Else 
Dim i 
For i = element_count To index + 1 Step -1 
Call InternalElement(i, vector_datas(i - 1)) 
Next 
Call InternalElement(index, element) 
End If 
element_count = element_count + 1 
If element_count = max_capacity Then 
max_capacity = element_count + capacity_increment 
ReDim Preserve vector_datas(max_capacity) 
End If 
End Function 

Public Function SetElementAt(index, element) 
If index < 0 Or index > element_count - 1 Then 
Err.Raise 20903, "Vector", "Array Index Out Of Bounds.", "", 0 
End If 
Call InternalElement(index, element) 
End Function 

Private Function InternalElement(index, element) 
On Error Resume Next 
If IsObject(element) Then 
Set vector_datas(index) = element 
Else 
vector_datas(index) = element 
End If 
If Err.Number <> 0 Then 
MsgBox("Vector InternalElement Error: " & vbCrLf & "Error Source: " & Err.Source & vbCrLf & "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf) 
Err.Clear '清除错误信息 
End If 
End Function 

Private Sub Class_Terminate() '类销毁 
Erase vector_datas '释放数组占用的内存, 將每個元素都設為 Nothing 
initial_capacity = Empty 
capacity_increment = Empty 
element_count = Empty 
max_capacity = Empty 
End Sub 

End Class 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/o1o2o3o4o5/archive/2009/10/20/4703033.aspx

标签:asp,动态数组
0
投稿

猜你喜欢

  • 使用 WinHttpRequest 伪造 Referer (附实战代码)

    2010-08-24 18:28:00
  • ActionScript3.0是革命性的

    2008-05-01 12:36:00
  • SQL Server 数据库操作实用技巧锦集

    2009-01-20 13:20:00
  • DW MX 2004新功能:加密FTP

    2009-09-13 18:42:00
  • 301转向和网址规范化

    2007-09-26 14:00:00
  • 触手生春【4.13】CSS中的伪元素选择符

    2008-11-11 13:10:00
  • CSS制作圆角导航的另一思路

    2008-11-06 11:39:00
  • ASP常见的保留字整理(变量与表名注意不能用)

    2013-06-01 19:58:01
  • ASP连接MSSQL2005 数据库

    2009-03-08 19:20:00
  • asp如何显示存储在数据库BLOB字段中的图像?

    2010-06-08 09:31:00
  • 在Asp程序中取得表单所有内容的方法

    2010-04-24 16:07:00
  • JavaScript版俄罗斯方块Easy Tetris实现原理

    2009-07-16 10:25:00
  • asp程序运行速度测试

    2008-02-11 19:11:00
  • 浅析facebook的信息架构

    2008-07-25 19:57:00
  • 教你设计大型Oracle数据库

    2009-07-02 12:31:00
  • 最简洁的多重查询的解决方案

    2008-03-02 15:51:00
  • SQL语句之WHERE子句的使用方法

    2007-08-11 12:25:00
  • asp源码如何显示数据库字段的结构?

    2010-06-08 09:35:00
  • ajax xmlhttp getResponseHeader实例教程

    2009-02-04 10:46:00
  • 用Dreamweaver 4.0编制会议通知

    2010-10-20 20:06:00
  • asp之家 网络编程 m.aspxhome.com