2016-09-21 77 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