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

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

问题描述

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.

<小时>

有用的链接

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

@Bond in the comments below came up with the most convincing one in my opinion for using Dim output().

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.

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.


Useful Links

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

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