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