Skip to content Skip to footer

VBA - 数组

VBA - 数组

上一页

下一页

我们非常清楚,变量是用于存储值的容器。有时,开发人员需要在一个变量中同时存储多个值。当一系列值存储在一个变量中时,它被称为数组变量。

数组声明

数组的声明方式与变量的声明方式相同,只是数组变量的声明使用了括号。在下面的示例中,数组的大小在括号中指定。

'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

执行上述函数时,会产生以下输出。

Value stored in Array index 0 : 1

Value stored in Array index 1 : VBScript

Value stored in Array index 2 : 100

Value stored in Array index 3 : 2.45

Value stored in Array index 4 : 7/10/2013

Value stored in Array index 5 : 12:45:00 PM

多维数组

数组不仅限于一维,还可以最多有 60 维。二维数组是最常用的数组。

示例

在下面的示例中,声明了一个具有 3 行 4 列的多维数组。

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

执行上述函数时,会产生以下输出。

Value stored in Array index : 0 , 1 : Orange

Value stored in Array index : 2 , 2 : coffee

ReDim 语句

ReDim 语句用于声明动态数组变量并分配或重新分配存储空间。

语法

ReDim [Preserve] varname(subscripts) [, varname(subscripts)]

参数描述

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

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

执行上述函数时,会产生以下输出。

XYZ

41.25

22

3

4

5

6

7

数组方法

VBScript 中有各种内置函数可以帮助开发人员有效地处理数组。下面列出了所有与数组一起使用的方法。请单击方法名称以详细了解它。

序号

函数和描述

1

LBound

一个函数,返回一个整数,该整数对应于给定数组的最小下标。

2

UBound

一个函数,返回一个整数,该整数对应于给定数组的最大下标。

3

Split

一个函数,返回一个包含指定数量值的数组。基于分隔符拆分。

4

Join

一个函数,返回一个字符串,该字符串包含数组中指定数量的子字符串。这是 Split 方法的完全相反的函数。

5

Filter

一个函数,返回一个基于零的数组,该数组包含基于特定筛选条件的字符串数组的子集。

6

IsArray

一个函数,返回一个布尔值,指示输入变量是否为数组。

7

Erase

一个函数,回收数组变量的已分配内存。

打印页面

上一页 下一页

广告

Copyright © 2088 天使游戏开发中心 - 独立游戏活动资讯 All Rights Reserved.
友情链接