0

Background: As a Java programmer, in order to familiarize myself with VBA, I am creating a VBA implementation of the Java ArrayList. The class is almost complete, but I am having trouble implementing the remove(int index) method.

Question: How do I empty a single element in an array.

In Java, I would use arr[index] = null. In VBA, I tried Set arr[index] = Nothing. The result was that the index-th element was not empty, but a Nothing Object (see the screenshot of my immediate window below). So, what is the proper syntax to ensure that elementData(0) is Empty at the end of my function call?

Immediate Window

My full function code is below.

Public Function removeIndex(index As Long) As Variant
    rangeCheck (index) 'verify that index is in-bounds

    modCount = modCount + 1

    Dim oldValue As Variant
    oldValue = elementData(index) 

    Dim numMoved As Long
    numMoved = size - index - 1
    If numMoved > 0 Then
        'move the remaining elements left one
        arrayCopy elementData, index + 1, elementData, index, numMoved
    End If

    size = size - 1
    Set elementData(size) = Nothing
    remove = oldValue
End Function
Austin
  • 8,018
  • 2
  • 31
  • 37

0 Answers0