2016-09-21 80 views
1

在VB6我有一個這樣定義的動態數組,因爲我不知道有多少價值添加運行程序VB6如何調整動態數組

Private myrray() As String 

現在我有問題,這REDIMENSION而數組做。我曾嘗試以下

Dim Upper As Integer 
Upper = UBound(myArray) ' here I get the runtime error 
If Err.Number Then 
    Upper = 0 
Else 
Upper = UBound(myArray) + 1 
End If 

的問題是上() - 函數總是給我運行時錯誤9「有效區域

我也嘗試用其它方法外指標,在這裏,我成爲了同樣的錯誤,但在else區塊:

Dim Upper As Integer 
If IsEmpty(myArray) Then 
    Upper = 0 
Else 
    Upper = UBound(myArray) + 1 'here I get the same error in this line 
End If 

後來,我將使用到redimensionze我像數組這增加了新的價值:

ReDim Preserve myArray(Upper) 
myArray(Upper) = "new text" 

有誰知道爲什麼,我怎麼能解決這個問題,或者告訴我的其他的方式來redimensionize數組?

回答

1

(我假設一個錯字:你聲明myrraymyArray

Private myArray() As String

這給你一個未初始化數組,即一個與所有&沒有尺寸您的問題是,你不能叫狀陣列功能UBound/LBound在該狀態下的陣列上。

的選項有:

  • 大小設置爲0 redim myArray(0)應用程序啓動時
  • Detect and handle未初始化狀態,並相應地處理它
+0

你可以用'myArray的=斯普利特(vbNullString) '得到一個初始化但是空的數組。 – Bob77