一系列值存储在单个变量中时,则称为数组变量。
一、一维数组
数组声明
Method 1 : Using Dim
Dim arr1() 'Without Size
'Method 2 : Mentioning the Size
Dim arr2(5) 'Declared with size of 5
'Method 3 : using 'Array' Parameter
Dim arr3
arr3 = Array("apple","Orange","Grapes")
在上面代码中,
- 虽然数组大小被指定为
5
,但是当数组索引从零开始时,它可以保持6
个值。 - 数组索引不能是负数。
- VBScript数组可以在数组中存储任何类型的变量。因此,一个数组可以在一个数组变量中存储一个整数,字符串或字符。
赋值给数组
Private Sub Constant_demo_Click()
Dim arr(5)
arr(0) = "1" 'Number as String
arr(1) = "VBScript" 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
msgbox("Value stored in Array index 0 : " & arr(0))
msgbox("Value stored in Array index 1 : " & arr(1))
msgbox("Value stored in Array index 2 : " & arr(2))
msgbox("Value stored in Array index 3 : " & arr(3))
msgbox("Value stored in Array index 4 : " & arr(4))
msgbox("Value stored in Array index 5 : " & arr(5))
End Sub
二、多维数据
Private Sub Constant_demo_Click()
Dim arr(2,3) as Variant ' Which has 3 rows and 4 columns
arr(0,0) = "Apple"
arr(0,1) = "Orange"
arr(0,2) = "Grapes"
arr(0,3) = "pineapple"
arr(1,0) = "cucumber"
arr(1,1) = "beans"
arr(1,2) = "carrot"
arr(1,3) = "tomato"
arr(2,0) = "potato"
arr(2,1) = "sandwitch"
arr(2,2) = "coffee"
arr(2,3) = "nuts"
msgbox("Value in Array index 0,1 : " & arr(0,1))
msgbox("Value in Array index 2,2 : " & arr(2,2))
End Sub
三、ReDim语句
ReDim
语句用于声明动态数组变量并分配或重新分配存储空间。
ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Vb
参数说明
- Preserve - 一个可选参数,用于在更改最后一个维度的大小时保留现有数组中的数据。
- Varname - 必需的参数,表示变量的名称,应遵循标准变量命名约定。
- Subscripts - 必需的参数,表示数组的大小。
Private Sub Constant_demo_Click()
Dim a() As Variant
i = 0
ReDim a(5)
a(0) = "XYZ"
a(1) = 41.25
a(2) = 22
a(3) = 34
a(4) = 44
MsgBox a(4)
ReDim Preserve a(7)
For i = 3 To 7
a(i) = i
Next
'to Fetch the output
For i = 0 To UBound(a)
MsgBox a(i)
Next
End Sub
结果
44,XYZ,41.25,22,3,4,5,6,7
四、数组方法
1、LBound()
函数返回指定数组的最小下标。 因此,数组的LBound
的结果是零
Private Sub Constant_demo_Click()
Dim arr(5) as Variant
arr(0) = "1" 'Number as String
arr(1) = "VBScript 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
msgbox("The smallest Subscript value of the given array is : " & LBound(arr))
' For MultiDimension Arrays :
Dim arr2(3,2) as Variant
msgbox("The smallest Subscript of the first dimension of arr2 is : " & LBound(arr2,1))
msgbox("The smallest Subscript of the Second dimension of arr2 is : " & LBound(arr2,2))
End Sub
2、LBound()
函数返回指定数组的最大下标。 因此,这个值对应于数组的大小。
Private Sub Constant_demo_Click()
Dim arr(5) as Variant
arr(0) = "1" 'Number as String
arr(1) = "VBScript 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
msgbox("The smallest Subscript value of the given array is : " & UBound(arr))
' For MultiDimension Arrays :
Dim arr2(3,2) as Variant
msgbox("The smallest Subscript of the first dimension of arr2 is : " & UBound(arr2,1))
msgbox("The smallest Subscript of the Second dimension of arr2 is : " & UBound(arr2,2))
End Sub
3、Split()
函数返回一个数组,其中包含基于分隔符分割的特定数量的值。
语法
Split(expression[,delimiter[,count[,compare]]])
Vb
参数说明
- Expression - 必需的参数。可以包含带分隔符的字符串的字符串表达式。
- Delimiter - 一个可选参数。该参数用于根据分隔符转换为数组。
- Count - 一个可选参数。要返回的子字符串的数量,如果指定为
-1
,则返回所有子字符串。 - Compare - 一个可选参数。该参数指定要使用哪种比较方法。
- 0 = vbBinaryCompare - 执行二进制比较
- 1 = vbTextCompare - 执行文本比较
Private Sub Constant_demo_Click()
' Splitting based on delimiter comma '$'
Dim a as Variant
Dim b as Variant
a = Split("Red $ Blue $ Yellow","$")
b = ubound(a)
For i = 0 to b
msgbox("The value of array in " & i & " is :" & a(i))
Next
End Sub
4、join
Join()
函数返回一个包含数组中指定数量的子串的字符串。这是Split
方法的一个完全相反的功能。
Join(List[,delimiter])
Vb
参数说明
- List - 必需的参数。包含要连接的子字符串的数组。
- Delimiter - 一个可选参数。该参数用于根据分隔符转换为数组。
Private Sub Constant_demo_Click()
' Join using spaces
a = array("Red","Blue","Yellow")
b = join(a)
msgbox("The value of b " & " is :" & b)
' Join using $
b = join(a,"$")
msgbox("The Join result after using delimiter is : " & b)
End Sub
5、filter
Filter()
函数返回一个基于零的数组,其中包含基于特定过滤条件的字符串数组的子集。
语法
Filter(inputstrings,value[,include[,compare]])
Vb
参数说明
- Inputstrings - 必需的参数。该参数对应于要搜索的字符串数组。
- Value - 必需的参数。此参数对应于要根据
inputstrings
参数搜索的字符串。 - Include - 一个可选参数。这是一个布尔值,它指示是否返回包含或排除的子字符串。
- Compare - 一个可选参数。该参数描述要使用哪种字符串比较方法。
- 0 = vbBinaryCompare - 执行二进制比较
- 1 = vbTextCompare - 执行文本比较
Private Sub Constant_demo_Click()
Dim a,b,c,d as Variant
a = array("Red","Blue","Yellow")
b = Filter(a,"B")
c = Filter(a,"e")
d = Filter(a,"Y")
For each x in b
msgbox("The Filter result 1: " & x)
Next
For each y in c
msgbox("The Filter result 2: " & y)
Next
For each z in d
msgbox("The Filter result 3: " & z)
Next
End Sub
6、isArray()
IsArray()
函数返回一个布尔值,指示指定的输入变量是否是数组变量。
语法
IsArray(variablename)
Private Sub Constant_demo_Click()
Dim a,b as Variant
a = array("Red","Blue","Yellow")
b = "12345"
msgbox("The IsArray result 1 : " & IsArray(a))
msgbox("The IsArray result 2 : " & IsArray(b))
End Sub
7、Erase()
Erase()
函数用于重置固定大小数组的值并释放动态数组的内存。 它的行为取决于数组的类型。
语法
Erase ArrayName
Vb
- 固定数值数组,数组中的每个元素重置为零。
- 固定字符串数组,数组中的每个元素被重置为零长度
""
。 - 对象数组,数组中的每个元素被重置为特殊值
Nothing
。
Private Sub Constant_demo_Click()
Dim NumArray(3)
NumArray(0) = "VBScript"
NumArray(1) = 1.05
NumArray(2) = 25
NumArray(3) = #23/04/2013#
Dim DynamicArray()
ReDim DynamicArray(9) ' Allocate storage space.
Erase NumArray ' Each element is reinitialized.
Erase DynamicArray ' Free memory used by array.
' All values would be erased.
msgbox("The value at Zeroth index of NumArray is " & NumArray(0))
msgbox("The value at First index of NumArray is " & NumArray(1))
msgbox("The value at Second index of NumArray is " & NumArray(2))
msgbox("The value at Third index of NumArray is " & NumArray(3))
End Sub
结果: