Removing an element of an array
I have created a type class which holds a line item detail record for an order. I have created an array of this type. I wish to be able to, at any time, remove an element of this array. I guess what I'm trying to say is....how do you remove an element from an array other than clearing out the values of each field within the type class?
Thanks,
Blake
Re: Removing an element of an array
You can't really delete an item in the middle of the array, but what you can do is to "shift" down and override the item you want to delete, then delete the last item with ReDim Preserve keywords.
But in cases like this I would rather use a collection, they are made for this purpose, to add/delete in the collection at any index.
Re: Removing an element of an array
You would have to recreate the whole array, but just not add the deleted entry.
or....you could use a collection.
Code:
Option Explicit
Private mcolItems As Collection
Private Sub Form_Load()
Set mcolItems = New Collection
'add items
mcolItems.Add "Woof"
mcolItems.Add "Moose"
mcolItems.Add "Badger"
mcolItems.Add "Growl"
'delete item 2
mcolItems.Remove 2
'show new item in pos 2
MsgBox mcolItems.Item(2)
Set mcolItems = Nothing
End Sub
I use collections all over the place for storing data like this. Very easy, and very handy. A little slower that arrays, but neglegable for small amount of data...
Hope that helps.
Woka
Re: Removing an element of an array
I was talking about something like this (when "shifting" the data):
VB Code:
Option Explicit
Private Sub Form_Load()
Dim strArray() As String, K As Long
ReDim strArray(5)
strArray(0) = "a"
strArray(1) = "b"
strArray(2) = "c"
strArray(3) = "d"
strArray(4) = "e"
strArray(5) = "f"
Debug.Print "Array UBound: " & UBound(strArray)
For K = 0 To UBound(strArray)
Debug.Print "strArray(" & K & ") = " & strArray(K)
Next K
RemoveItem strArray, 2 ' remove "c"
Debug.Print
Debug.Print
Debug.Print "Array UBound: " & UBound(strArray)
For K = 0 To UBound(strArray)
Debug.Print "strArray(" & K & ") = " & strArray(K)
Next K
End Sub
Private Sub RemoveItem(sArray() As String, ByVal Index As Long)
Dim K As Long
If Index < UBound(sArray) Then
For K = Index To UBound(sArray) - 1
sArray(K) = sArray(K + 1)
Next K
End If
ReDim Preserve sArray(UBound(sArray) - 1)
End Sub
Re: Removing an element of an array
Yea, that's what I meant.
I just got my words mixed up :D
Bad Woka *slap*
Woof
Re: Removing an element of an array
Ok,
Let's say that I want to remove the 1st element of the array....what would the code look like?
Re: Removing an element of an array
errr.
Just use CVMichaels code:
Code:
Private Sub RemoveItem(sArray() As String, ByVal Index As Long)
Dim K As Long
If Index < UBound(sArray) Then
For K = Index To UBound(sArray) - 1
sArray(K) = sArray(K + 1)
Next K
End If
ReDim Preserve sArray(UBound(sArray) - 1)
End Sub
Pass the array to this function, and the Index of the element you want to remove.
In CVMichaels example they used:
Code:
RemoveItem strArray, 2 ' remove "c"
To remove the item with an index of 2 (this is the 3rd item since the 1st index is 0).
So if you want to remove the 1st item then do:
Code:
RemoveItem strArray, 0
Woka