CodeItBetter Programming Another VB Programming Blog

How to delete an item in an array

Posted on October 23, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'Arrays, Collections, Lists - How to delete an item in an array
Sub DeleteArrayItem(arr As Variant, index As Long)
    Dim I As Long
    For I = index To UBound(arr) - 1
        arr(I) = arr(I + 1)
    Next
    ' VB will convert this to 0 or to an empty string.
    arr(UBound(arr)) = Empty
End Sub
 
'Or (Using API)

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _
    source As Any, ByVal numBytes As Long)
 
Sub DeleteArrayItemLong(arr() As Long, index As Long)
    If index < UBound(arr) Then
        CopyMemory arr(index), arr(index + 1), (UBound(arr) - index) * LenB(arr(index))
    End If
    arr(index) = Empty
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.