LotusScript ans二维数组&订阅输出或范围错误
问题描述:
你好,我在LotusScript中有如下的二维数组。LotusScript ans二维数组&订阅输出或范围错误
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)
答
除了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
答
您正在使用带Preserve选项的ReDim并更改了两个尺寸。你不能那样做。
从documentation for the ReDim statement:
如果保护区被指定,你只能改变上限 最后阵列尺寸。尝试更改 中的任何其他绑定结果时出错。
此外,那里的逻辑被搞砸了。在每次迭代中,你都要做三次redim,第一次在每次迭代时将第二维缩小到零。即使您没有更改第一维,也会丢失存储在AllRecrods(n,1)中的数据,因为preserve选项无法将数据保存在缩小到您已经使用的大小以下的维中!
您应该考虑交换您的两个维度,在您的任务中颠倒它们,保持第一维常数为2,并删除两个ReDim Preserve语句。即,在循环的每次迭代中只做一个ReDim Preserve AllRecrods(2,counter)
。
感谢你们俩:) – hdc