LotusScript 和二维数组和下标超出范围错误

LotusScript ans Two dimensional Array & subscription out or range error

您好,我在 LotusScript 中有如下二维数组。

Counter = 0
While Not (ProcessingViewDoc Is Nothing )
    Redim Preserve AllRecrods(Counter,0)  
    AllRecrods(Counter,0)   = ProcessingViewDoc.Test1(0)
    Redim Preserve AllRecrods(Counter,1)  
    AllRecrods(Counter,1)   = ProcessingViewDoc.Test2(0)
    Redim Preserve AllRecrods(Counter,2)  

    Set ProcessingViewDoc  = ProcessingView.GetNextDocument(ProcessingViewDoc)
    Counter = Counter +1
Wend

当它处理下一个文档并到达计数器 1 和第二个文档时,它给我错误订阅超出范围。 这是数组的全局声明。

Dim AllRecrods() As Variant

这是第二次循环报错的那一行

Redim Preserve AllRecrods(Counter,0) 

您正在使用带有“保留”选项的 ReDim 并更改两个尺寸。你不能那样做。

来自documentation for the ReDim statement

If Preserve is specified, you can change only the upper bound of the last array dimension. Attempting to change any other bound results in an error.

另外,逻辑也搞砸了。您在每次迭代中都进行了三次 redim,第一个在每次迭代中将第二个维度缩小回零。即使您没有更改第一个维度,这也会丢失您存储在 AllRecrods( n ,1) 中的数据,因为保留选项无法将数据保存在您缩小到已使用的大小以下的维度中!

您可能应该考虑交换两个维度,在您的作业中将它们反转,将第一个维度保持为 2,并消除两个 ReDim Preserve 语句。即,在循环的每次迭代中只做一个 ReDim Preserve AllRecrods(2,counter)

除了 Richard 的出色回答外,我还提出了一些建议。

1) 使用 Do Until doc Is Nothing 而不是 While Not (ProcessingViewDoc Is Nothing)(包含两个否定,使其更难阅读)。清晰多了。

2) 如果使用列表,则不必担心数组的redim。您可以将其设为自定义数据类型的列表,如果您使用文档的 UNID 作为键,则可以快速将值连接回原始文档。

我的代码看起来像这样:

--- Declarations ---
Type recordData
    value1 As String
    value2 As String
End Type


--- Main Code ---
Dim allRecords List As recordData
Dim unid as String
Do Until ProcessingViewDoc Is Nothing 
    unid = ProcessingViewDoc.UniqueID
    allRecords(unid).value1 = ProcessingViewDoc.Test1(0)
    allRecords(unid).value2 = ProcessingViewDoc.Test2(0)
    Set ProcessingViewDoc  = ProcessingView.GetNextDocument(ProcessingViewDoc)
Loop