声明动态数组未按预期工作 [英] Declaring a dynamic array not working as expected
问题描述
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(recCount,2)
我收到一个错误,因为recCount类型错误.我试图将其转换为Int
,但这也不起作用.以下工作正常:
I get an error because recCount is of wrong type. I have tried to convert it to Int
but that does not work either. The following works fine:
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(3,2)
如何转换recCount
以使该数组声明起作用?
How do I convert recCount
to get this array declaration to work?
推荐答案
您需要先将Array声明为动态数组,然后使用ReDim
这样动态设置第一维;
You need to first declare your Array as dynamic then use ReDim
to set the first dimension dynamically, like this;
Dim output() 'Declare a "Dynamic Array"
ReDim output(recCount, 2) 'Set dynamic first dimension.
通过指定这样的尺寸;
By specifying the dimensions like this;
Dim output(10, 2) 'Declare a "Fixed Array"
您告诉VBScript构建一个不接受变量作为维声明的固定数组.
you tell VBScript to build a fixed array which does not accept variables as dimension declarations.
似乎有一些关于使用的争论
There seems to be some argument as to using
Dim output()
与
Dim output
@Bond in the comments below came up with the most convincing one in my opinion for using
Dim output()
.
因为VBScript将所有变量存储为Variant
数据类型,所以使用Dim output
声明Array
就像声明任何其他变量一样,而Dim output()
创建一个Array
(大小为0的数组,但是仍然是数组),这意味着就VBScript运行时而言,有一个显着的区别,Dim output()
是Array
.
Because VBScript stores all variables as Variant
data type declaring an Array
using Dim output
is just like declaring any other variable, whereas Dim output()
creates an Array
(0 sized array but an array nonetheless), this means there is one significant difference as far as the VBScript Runtime is concerned, Dim output()
is an Array
.
这有什么意义?那很重要,因为像IsArray()
这样的函数仍会将变量检测为Array
,其中使用Dim output
声明的数组"将返回False
.
What does this matter?, well it matters because functions like IsArray()
will still detect the variable as an Array
where as "arrays" declared using Dim output
will return False
.
有用链接
-
@DougGlancy对Redim不带Dim的回答?(vba但仍很重要).
@DougGlancy's answer to Redim without Dim? (vba but still relevant).
VBScript参考-VBScript变量(特别是数组变量"子标题).
VBScript Reference - VBScript Variables (specifically Array Variables sub heading).
这篇关于声明动态数组未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!