【问题标题】:Declaring a dynamic array not working as expected声明动态数组未按预期工作
【发布时间】:2014-09-26 12:24:34
【问题描述】:
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(recCount,2)

我收到一个错误,因为 recCount 类型错误。我试图将其转换为Int,但这也不起作用。以下工作正常:

RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(3,2)

如何转换 recCount 以使此数组声明生效?

【问题讨论】:

  • dim recCount:recCount ????当然不是在 VB.NET 中
  • 您应该尝试Dim recCount = CInt(RsProxyList.RecordCount) 并确保检索不到超过 32767 条记录
  • @Steve 您不能像这样在 Classic ASP 中声明和分配,而且 OP 已经说过他们 “试图将其转换为 int 但确实如此也不行”.
  • .RecordCount 已经是integer 问题是您无法动态设置固定数组的维度,这一切都归结为数组的声明方式。
  • 这个数组有什么用?,你知道如果你想从ADODB.Recordset 填充一个数组,你可以使用.GetRows() 方法,它返回一个二维的列和行数组?

标签: arrays vbscript asp-classic dynamic-arrays


【解决方案1】:

您需要先将您的 Array 声明为动态的,然后使用 ReDim 动态设置第一个维度,如下所示;

Dim output() 'Declare a "Dynamic Array"
ReDim output(recCount, 2) 'Set dynamic first dimension.

通过像这样指定尺寸;

Dim output(10, 2) 'Declare a "Fixed Array"

您告诉 VBScript 构建一个不接受变量作为维度声明的固定数组。


Dim output()Dim output

似乎有一些关于使用的争论

Dim output()

Dim output

comments below 中的@Bond 提出了我认为使用Dim output() 最有说服力的一个。

因为 VBScript 将所有变量存储为 Variant 数据类型,使用 Dim output 声明 Array 就像声明任何其他变量一样,而 Dim output() 创建一个 Array(0 大小的数组但仍然是一个数组),这意味着就 VBScript 运行时而言存在一个显着差异,Dim output()Array

这有什么关系?这很重要,因为像IsArray() 这样的函数仍会将变量检测为Array,而使用Dim output 声明的“数组”将返回False


有用的链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多