Array or Collection

Started by MacGyver, September 12, 2012, 06:38:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MacGyver

What are the advantages/disadvantages of using a Collection instead of an Array.

Previously I have used mainly arrays in my vb programs.  Often these arrays are of non-custom objects (i.e. Strings, Longs...).  I am currently delving into the world of creating my own objects and was wondering what sorts of advantages a collection might have over an array.  Just briefly looking into it, i noticed i want have access to some functions I use regularly on my arrays such as Filter()

Jumpy

Some points that come to mind (if pro or con you have to decide for yourself)

- No need for ReDim and such, just Add an object
- Collection.Count gives number of objects in Collection
- UBound(Array) gives max number of objects in array, but there's no gurantee, that there is realy an object at a given position in array
  Array(UBound(Array)) could not be filled yet
- You can use For Each Syntax to loop through elements in Collection
- You can place different kind of objects in the same collection, but you'll have to keep track, what type of object is at what position
- You could create your own collection class that incapsulates a normal collection but offers additional methods, for example a custom sort