问题描述
RsProxyList.Open objDBCommand,,1,1暗淡 recCount:recCount = RsProxyList.RecordCount昏暗的输出(recCount,2)
我收到一个错误,因为 recCount 的类型错误.我试图将其转换为 Int
但这也不起作用.以下工作正常:
RsProxyList.Open objDBCommand,,1,1暗淡 recCount:recCount = RsProxyList.RecordCount昏暗的输出(3,2)
如何转换 recCount
以使此数组声明起作用?
你需要先声明你的 Array 为 dynamic 然后使用 ReDim
动态设置第一维,就像这样;
Dim output() '声明一个动态数组"ReDim output(recCount, 2) '设置动态第一维.
通过像这样指定尺寸;
Dim output(10, 2) '声明一个固定数组"
您告诉 VBScript 构建一个不接受变量作为维度声明的固定数组.
Dim output()
对比 Dim output
似乎有一些关于使用的争论
Dim output()
对比
暗淡输出
@Bond 在 下面的评论是我认为最有说服力的使用Dim output()
的评论.
因为 VBScript 将所有变量存储为 Variant
数据类型,所以使用 Dim output
声明一个 Array
就像声明任何其他变量一样,而 Dim output()
创建一个 Array
(0 大小的数组,但仍然是一个数组),这意味着就 VBScript 运行时而言存在一个显着差异,Dim 输出()
是一个 Array
.
这有什么关系?,这很重要,因为像 IsArray()
这样的函数仍然会将变量检测为 Array
而作为使用 声明的数组"Dim 输出
将返回False
.
有用的链接
@DougGlancy 对 Redim without Dim 的回答?(vba 但仍然相关).
VBScript 参考 - VBScript 变量(特别是数组变量子标题).
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(recCount,2)
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)
How do I convert recCount
to get this array declaration to work?
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"
you tell VBScript to build a fixed array which does not accept variables as dimension declarations.
Dim output()
versus Dim output
There seems to be some argument as to using
Dim output()
versus
Dim output
Useful Links
@DougGlancy's answer to Redim without Dim? (vba but still relevant).
VBScript Reference - VBScript Variables (specifically Array Variables sub heading).
这篇关于声明一个动态数组没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!