|
-
Oct 12th, 2002, 03:26 PM
#1
Thread Starter
Fanatic Member
Array Q!
Hi Again.
I'm sorry but I have this annoying question:
I have an array: 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:
Redim Preserve MyArray(1 to 4)
It gives me an error.
Thank you in advance,
Arie.
-
Oct 12th, 2002, 04:36 PM
#2
Good Ol' Platypus
You need to do this:
VB Code:
Dim nDelete As Long
Dim i As Long
nDelete = 5 'The index you want to KILL! :)
For i = nDelete + 1 To UBound(MyArray)
MyArray(i - 1) = MyArray(i)
Next i
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)
-
Oct 13th, 2002, 08:31 AM
#3
Thread Starter
Fanatic Member
Thank you,
I'll try it, but I think that that's what I have done and it gave me an error.
Arie.
-
Oct 13th, 2002, 02:14 PM
#4
Thread Starter
Fanatic Member
It gave me an error: 'This array is fixed or temporarily locked'
Anyone knows the meaning?
Please help!!
Thank you,
Arie.
-
Oct 13th, 2002, 03:01 PM
#5
Frenzied Member
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.
-
Oct 13th, 2002, 03:18 PM
#6
Good Ol' Platypus
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)
-
Oct 13th, 2002, 03:19 PM
#7
Good Ol' Platypus
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)
-
Oct 14th, 2002, 01:52 PM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|