Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single dim array: Remove empty element 1

Status
Not open for further replies.

CP60

Programmer
Oct 16, 2008
145
DE

Single dimensioned array, type string

Say the array has 3 element:

Redim aryString(0 to 2) as String
Debug.Print Ubound(aryString) = 2 'True

Element 0 and 2 have data and element 1 doesn't, and I want to remove that element, so that at the end there are only 2 elements in the array:
Debug.Print Ubound(aryString) = 1 'True

In VB6 I used CopyMemory and VarPtr and then Redim to remove a null string element of an array.

How do I do this in VB.NET? Is there already some function available?
Or would I just use a "for each" and transfer the elements to a new array and then redim the original and copy the back?
 

I suggest that rather than an array you use a collection instead. Collections operate almost identically to arrays, but you use the Add and Remove methods to insert and delete elements. The beauty is that when you use the Remove and Add methods, the collection is automatically re-sized, no ReDim required.

Just Google 'vb .net collection class' (without quotes) to get plenty of info on how to use them.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks, but I want/need to do this with an array for now.

(I know about the collections and how to use them and do use them.)

I have an old app with too much old code (several hundred thousands of code lines originally created in VB6), to change this over everywhere at once.

I could use
Private Declare Function VarPtrAny Lib "msvbvm60.dll" Alias "VarPtr" (lpObject As Object) As Long
as a replacement for the VarPtr() for now (it works), though I want to get away from things like this and also from Interop.

I was only hoping that there would be a VB.NET function which could do this.
 
Ah, I see. Sorry, there's no such functionality in .NET (except for a JScript function, but that won't help here).


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Would this work for you?

Dim N As Integer = ItemToRemove
'assumes N is the position In Array to remove
For i As Integer = N To UBound(YourArray) - 1
YourArray(i) = YourArray(i + 1)
Next
ReDim Preserve YourArray(UBound(YourArray) - 1)
 
waytech2003,
So simple! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top