Results 1 to 8 of 8

Thread: Array Q!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Israel
    Posts
    636

    Question Array Q!

    Hi Again.

    I'm sorry but I have this annoying question:

    I have an array:
    VB Code:
    1. Dim MyArray() as tMyType
    After a while it gets many cells. Say it's now at 1 to 5 size.
    I want to delete a middle cell, I delete it and I want to refix the size to 1 to 4.
    How can I do it?
    When I do:
    VB Code:
    1. Redim Preserve MyArray(1 to 4)
    It gives me an error.

    Thank you in advance,
    Arie.

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You need to do this:
    VB Code:
    1. Dim nDelete As Long
    2. Dim i As Long
    3.  
    4. nDelete = 5 'The index you want to KILL! :)
    5. For i = nDelete + 1 To UBound(MyArray)
    6.     MyArray(i - 1) = MyArray(i)
    7. Next i
    8. Redim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - 1)
    It's pretty simple to work out what it does, I hope you'll ask again if you have any difficulties!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Israel
    Posts
    636
    Thank you,
    I'll try it, but I think that that's what I have done and it gave me an error.

    Arie.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Israel
    Posts
    636
    It gave me an error: 'This array is fixed or temporarily locked'
    Anyone knows the meaning?
    Please help!!

    Thank you,
    Arie.

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Arie, do an MSDN search for that error. It will tell you what you are doing wrong.

    Sas, a better way would be to copy the last element of the array into the slot you want to delete, and then shrink the array by one:
    Code:
    |2|4|3|1|5|
    remove the 4...
    |2|5|3|1|5|
    Shrink:
    |2|5|3|1|
    Your code is good for short arrays that need to remain sorted, but at times even the above method followed by a sort can be faster (by a bit, not much) =).

    Z.

  6. #6
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I like the 'clean' approach that the first one gives (which is why I always do code in roundabout ways )
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  7. #7
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I get that error when I dim an array like MyArray(1 to 5) and then try to redim it. Maybe that's what you're doing?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Israel
    Posts
    636
    What you mean is that:

    I create an array MyArray() without the limits
    and then Redim it and when I want to shrink it, I can't RediM it again?
    Maybe there is another way... Anyone?

    Thank you so far,
    Arie.

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