I'm working on an Excel VBA addin that exchanges objects with a COM server, something like this:
'get an array of objects
Dim Ents() As ISomething
ComObject.GetEntities Ents
'send an array with 10 objects
ReDim Ents(9)
Set Ents(0) = ...
...
ComObject.SetEntities Ents
Getting the arrays works well: if the array contains objects it works as expected, if the array is empty then UBound(Ents) = -1
and everything works as expected.
Sending the arrays works only with not empty arrays, because I can't Redim Ents(-1)
, and Erase
ing the array both VBA and the COM server crash: Debug.Print UBound(Ents)
crashes in VBA and who knows what crashes the server.
It looks like the Erase
statement leaves the array undefined/corrupted rather than empty.
EDIT (clarification to a comment below):
Executing this code it crashes because it can't calculate the UBound
:
Sub Test()
Dim Ents() As ISmartId
Debug.Print UBound(Ents)
End Sub
But if you add Ents
to the watch window, then set a break point to the Debug.Print
line and execute, the debugger shows the ISmartId(0 to -1)
in the Type column. After this the execution continues without crash, and the Debug window shows the expected -1
.
It looks like the debugger was able to correctly initialize the empty array the way I need it just to show its value.