TheProgrammingZone
Login:
Name:
Pass:
 
Home
 
 TheProgrammingZone's front page. All the latest programming and game development news, as well as this week's feature game project!
Articles
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
Other
Assembly aritcles
Source Code
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
Assembly
Game Projects
 
 Game projects under development by members of the TheProgrammingZone Community. Show them your support, and check out their games!
Message Board
 A forum to interact with your fellow programmers.
Links
 
Visual Basic
VB.Net
C/C++
C#
Java
PHP
ASP
JavaScript
Assembly Links
Other
Products
 
TheProgrammingZone's products. Check out some of our in house products that we made.
Contact
 
You want to contact the guy behind TheProgrammingZone? Here's where to do it!

 
 


Arrays or Collection objects? - page 1


pages: prev | 1 | next
With all versions of Visual Basic (and Visual Basic for Applications) after version 4, there are two possible ways to store a group of records - as an array or using the new collection class. Examples tend to use one extreme or the other - either putting everything in a collection or coding large arrays as if they had never heard of the collection class. In fact there are subtle differences between the two data types which should be considered when you are storing a group of records.

We'll start with the array. An array in Visual Basic is defined thus:

Dim as_Months(1 To 12) As String

In this example I have explicitly stated the lower bound. If you don't give the lower bound, Visual basic will use zero by default. You can override this by adding the following option to your code:

Option Base = 1

To set an element of an array you refer to it thus:

As_Months(3) = "March"

And to iterate through an array:

Dim lIndex As Long

For lIndex = Lbound(ar_Months) to Ubound(ar_Months)

Debug.Print ar_Months(lIndex)

Next Lindex

Note the use of the functions Lbound and Ubound - these return the lower and upper bounds of the array. If you try and access an index not in this range you will get an "Array Index out of bounds" error.

A collection, on the other hand, is a special type of object and is declared in the same manner as any other kind of object:

Dim colMonths As New Collection

To add an element:

colMonths.Add "January"

You can also specify 3 optional parameters, Key, Before and After which say where to insert the new element in the collection. The Key, if used, must uniquely identify this element.

To iterate through the collection:

Dim sMonth As String

For Each sMonth In colMonths

Debug.Print sMonth

Next sMonth

Notice that you do not have to worry about the boundaries of the collection - the For Each...Next syntax iterates through any size collection and you will never get an array out of bounds type error.

So - that's the use...what's the difference? The collection class is a great deal more flexible. By assigning unique keys to your items you can access them either by index or by key which is a very powerful programming technique. There is, however, a small speed and memory cost to this extra flexibility as compared to an array.

My advice would be: Use an array where - the number of items is constant or doesn't vary much, where access by key is not required and where speed is essential. Use a collection class where key lookup is needed, where you want your code to be more maintainable and where the ability to insert at a given point without having to write a complex element shuffling routine is needed.



pages: prev | 1 | next
TheProgrammingZone 2024
Site Map