5

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.

1
  • Why not use a List(Of Type) which is way more modern and easy to manipulate (LINQ etc...)? Commented Jul 26, 2015 at 19:37

2 Answers 2

18

use

Redim Preserve MyArray (UBound(MyArray) - 1)
Sign up to request clarification or add additional context in comments.

6 Comments

I think that's the solution ...yes it is
@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.
@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
@GSerg vb.net. At least that's on the course documentation :)
Yes, 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 is 0. But in VB6, it's often not. Which is a great, great thing.
|
6

VB6:

redim preserve arr(lbound(arr) to ubound(arr) - 1)

VB.NET:

redim preserve arr(arr.GetUpperBound(0) - 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.