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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove element from an anrray

Status
Not open for further replies.

dpimental

Programmer
Jul 23, 2002
535
US
Is there a way to remove an element from an array?
given the following ....

Code:
Dim arrTest (1 to 5) as int

arTest(1) = 1
arTest(2) = 2
arTest(3) = 3
arTest(4) = 4
arTest(5) = 5

How can I remove element 3 and resize the array such as this ...

Code:
arTest(1) = 1
arTest(2) = 2
arTest(3) = 4
arTest(4) = 5

I know I can do this in other languages. Can it be done in VB?

David Pimental
(US, Oh)
 
Not without, redimensioning the array and
redefining each element.

A collection on the other hand, does allow you to insert
and remove at will, in one manoeuvre.

For brevity, i'll post you these codes, i got from elsewhere,
I think a sight called "VBA Programmer"
_______________________________________________
Sub PlaceNewItem()
Dim vCollection As New Collection, x As Integer, sKey As Variant

sKey = Array("One", "two", "three", "four", "Five")

For x = 1 To UBound(sKey)
vCollection.Add sKey(x)
Next x

vCollection.Add 6, , 2 'places in the index, where to add new item. either before argument or after

For x = 1 To vCollection.Count
Debug.Print vCollection(x)
Next x


End Sub

________________________________________________________
Sub RemoveItem()
Dim vCollection As New Collection, x As Integer, sKey As Variant

sKey = Array("One", "two", "three", "four", "Five")

For x = 1 To UBound(sKey)
vCollection.Add sKey(x)
Next x

vCollection.Remove 2 'redimensions, automatically

For x = 1 To vCollection.Count
Debug.Print vCollection(x)
Next x


End Sub
________________________________________________________
 
That's what I thought. Is a collection like a dictionary? And does it only contain 2 elements, a key and a value?

David Pimental
(US, Oh)
 
A collection does contain only two values ... an item and (optionally) a key. The "item" can be just about anything however. For example, an instance of a user-defined class, an object, a TypeDef, etc. Collections and dictionary objects have some similarities but dictionary objects have considerably more flexibility and are usually faster than collections.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top