Some of the most common operations you perform on arrays are inserting and deleting items, shifting all the remaining elements toward higher indices to make room or toward lower indices to fill the “hole” a deletion has left. You usually do this with a For…Next loop, and you can even write generic array procedures that work with any type of array (with the usual restrictions about arrays of UDTs and fixed-length strings that can’t be passed to a Variant parameter):
Sub InsertArrayItem(arr As Variant, index As Long, newValue As Variant)
Dim i As Long
For i = UBound(arr) – 1 To index Step -1
arr(i + 1) = arr(i)
Next
arr(index) = newValue
End Sub
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
If your application works intensively with arrays, you might find that an approach based on For…Next loops is too slow. In some cases, you can considerably speed up these operations by using the RtlMoveMemory API function, which many Visual Basic programmers know under its popular alias name, CopyMemory.1 This function lets you move a block of bytes from one memory address to another memory address and works correctly even if the two areas partially overlap. Here’s the code that inserts a new item in an array of Longs:
Private Declare Sub CopyMemory Lib “kernel32″ Alias “RtlMoveMemory” _
(dest As Any, source As Any, ByVal numBytes As Long)
Sub InsertArrayItemLong(arr() As Long, index As Long, newValue As Long)
‘ We let VB evaluate the size of each item using LenB().
If index < UBound(arr) Then
CopyMemory arr(index + 1), arr(index), _
(UBound(arr) _ index) * LenB(arr(index))
End If
arr(index) = newValue
End Sub
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
IMPORTANT NOTE: The prerequisite for using the CopyMemory API function is that data must be stored in contiguous memory locations, so you absolutely can’t use it to insert or remove elements in String and Object arrays, nor in arrays of UDTs that contain conventional strings, object references, or dynamic arrays. (Fixed-length strings and static arrays in UDTs are OK, though.)
Note that while you can’t use the preceding routines for arrays other than Long arrays, the statements in the procedure body can be recycled for another data type without any change, thanks to the use of the LenB function. Therefore, you can derive new array functions that work for other data types by simply modifying the procedure’s name and its parameter list. For example, you can create a new function that deletes an item in a Double array by editing just the first line of code (shown in boldface):
Sub DeleteArrayItemDouble(arr() As Double, index As Long)
‘ All the other statements here are the same as in DeleteArrayItemLong
‘ …
End Sub

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Back
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 