声明动态数组未按预期工作 [英] Declaring a dynamic array not working as expected

查看:128
本文介绍了声明动态数组未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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's answer to Redim without Dim? (vba but still relevant).

VBScript参考-暗淡声明

VBScript参考-VBScript变量(特别是数组变量"子标题).

VBScript Reference - VBScript Variables (specifically Array Variables sub heading).

这篇关于声明动态数组未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆