2017-10-28 88 views
0

你好,我在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) 

回答

1

除了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 
1

您正在使用帶Preserve選項的ReDim並更改了兩個尺寸。你不能那樣做。

documentation for the ReDim statement

如果保護區被指定,你只能改變上限 最後陣列尺寸。嘗試更改 中的任何其他綁定結果時出錯。

此外,那裏的邏輯被搞砸了。在每次迭代中,你都要做三次redim,第一次在每次迭代時將第二維縮小到零。即使您沒有更改第一維,也會丟失存儲在AllRecrods(n,1)中的數據,因爲preserve選項無法將數據保存在縮小到您已經使用的大小以下的維中!

您應該考慮交換您的兩個維度,在您的任務中顛倒它們,保持第一維常數爲2,並刪除兩個ReDim Preserve語句。即,在循環的每次迭代中只做一個ReDim Preserve AllRecrods(2,counter)

+0

感謝你們倆:) – hdc