VB delete last element in dynamic array
14,990
Solution 1
use
Redim Preserve MyArray (UBound(MyArray) - 1)
Solution 2
VB6:
redim preserve arr(lbound(arr) to ubound(arr) - 1)
VB.NET:
redim preserve arr(arr.GetUpperBound(0) - 1)
Author by
YsoL8
I am a PHP Developer using CSS, Flash (actionscript 2) and some Javascript for presention. I am actively seeking to expand my knowledge of web development. Also I am actively seeking a job, so contact me if I could be suitable.
Updated on June 21, 2022Comments
-
YsoL8 almost 2 years
I need a way to delete the last element in an array when I don't know how big the array is.
Basically I need the VB version of php array_pop, but nothing relevant seems to be appearing in search results.
-
YsoL8 almost 13 yearsI think that's the solution ...yes it is
-
aelveborn almost 13 years@YsoL8 For VB6 it's not. The lower bound will be lost, and you'll get a runtime error. Please retag your question to clarify VB version.
-
heximal almost 13 years@GSerg: are you sure? I'm using this code for changing array length exactly in vb6 and have never faced the situation when lower array bound differs from 0
-
YsoL8 almost 13 years@GSerg vb.net. At least that's on the course documentation :)
-
aelveborn almost 13 yearsYes, I am. Try:
Dim arr() As Long : ReDim arr(1 To 10) : ReDim Preserve arr(UBound(arr) - 1)
. Run-time error 9: Subscript out of range. Of course, it'll work fine if the lower bound is0
. But in VB6, it's often not. Which is a great, great thing. -
devbf about 3 years@GSerg thanks a lot, your advice saved me after countless tries to find out why my ReDim is not working. Thought ReDim would determine the lower limit automatically, seems that that is not the case. First time using VBA in a while, still bugs me, that some functions return arrays which start with 1 instead of 0. Anyway, thanks a lot!