Results 1 to 7 of 7

Thread: Removing an element of an array

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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.

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Removing an element of an array

    I was talking about something like this (when "shifting" the data):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim strArray() As String, K As Long
    5.     ReDim strArray(5)
    6.    
    7.     strArray(0) = "a"
    8.     strArray(1) = "b"
    9.     strArray(2) = "c"
    10.     strArray(3) = "d"
    11.     strArray(4) = "e"
    12.     strArray(5) = "f"
    13.    
    14.     Debug.Print "Array UBound: " & UBound(strArray)
    15.     For K = 0 To UBound(strArray)
    16.         Debug.Print "strArray(" & K & ") = " & strArray(K)
    17.     Next K
    18.    
    19.     RemoveItem strArray, 2 ' remove "c"
    20.    
    21.     Debug.Print
    22.     Debug.Print
    23.    
    24.     Debug.Print "Array UBound: " & UBound(strArray)
    25.     For K = 0 To UBound(strArray)
    26.         Debug.Print "strArray(" & K & ") = " & strArray(K)
    27.     Next K
    28. End Sub
    29.  
    30. Private Sub RemoveItem(sArray() As String, ByVal Index As Long)
    31.     Dim K As Long
    32.    
    33.     If Index < UBound(sArray) Then
    34.         For K = Index To UBound(sArray) - 1
    35.             sArray(K) = sArray(K + 1)
    36.         Next K
    37.     End If
    38.    
    39.     ReDim Preserve sArray(UBound(sArray) - 1)
    40. End Sub

  5. #5

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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?

  7. #7
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width