VB6 redim 错误 "This array is fixed or temporarily locked"

VB6 redim error "This array is fixed or temporarily locked"

我有一个以 3 个对象值开头的全局数组变量 g ()。 然后我调用一个 sub,它使用 g 中的项目之一作为输入,并且需要在 g 中创建其他项目,并更新提供的项目。

类似于

声明:

Public g() As branch

初始化:

ReDim g (1 To 3)
Set g(1) = br1
Set g(2) = br2
Set g(3) = br3

sub

的代码调用

Call chg (g(2))

Public Sub chg (ByRef br As branch)
r = UBound(g)
ReDim g (1 To r + 2)
... (rest of the code)
End Sub

Redim 语句中的代码错误,错误文本为 "This array is fixed or temporarily locked"。

为什么我不能更改此子中数组的大小?有什么不同?

来自 MSDN 文档:

You tried to redimension a module-level dynamic array, in which one element has been passed as an argument to a procedure. For example, in the following code, ModArray is a dynamic, module-level array whose forty-fifth element is being passed by reference to the Test procedure.

There is no need to pass an element of the module-level array in this case, since it's visible within all procedures in the module. However, if an element is passed, the array is locked to prevent a deallocation of memory for the reference parameter within the procedure, causing unpredictable behavior when the procedure returns.

Dim ModArray() As Integer    ' Create a module-level dynamic array.

Sub AliasError()
   ReDim ModArray(1 To 73) As Integer
   Test ModArray(45)    ' Pass an element of the module-level array to the Test procedure.
End Sub

Sub Test(SomeInt As Integer)
   ReDim ModArray (1 To 40) As Integer  ' Error occurs here.
End Sub

一个想法是传递数组的索引而不是对象本身。