COMPARING CUSTOM COLLECTIONS TO ARRAYS

Now you know the basics for declaring a new collection and for adding and removing elements from the collection. In the following sections, you create a simple collection of integers, as well as an array of integers. The array takes three elements, calculates the number of elements, removes an element, and displays all the elements. You should recognize the advantages of collections at the completion of this example.

Creating a Collection of Integers

The code in Listing 4.1 declares a new collection by using the New keyword, adds elements to the collection, accesses an element of the collection, lists the elements of the collection, removes an element from the collection, and then displays the number of elements in the collection. In short, Listing 4.1 demonstrates each major feature of collections. Outside remembering to use the variant data type, the code is quite intuitive and easy to follow. You can find the ShowCollectionFeatures() function in the DisplayCollectionFeatures module.

Listing 4.1. Chap04.mdb: Demonstrating a Collection
Sub ShowCollectionFeatures()

   '1. Declare the new collection
   Dim colDemo as New Collection, varElement as Variant

   '2. Add 3 elements to the collection
   colDemo.Add 10
   colDemo.Add 20
   colDemo.Add 30

   '3. Print the 2nd element to the debug window
   Debug.Print colDemo(2)     ' Displays 20 in the debug window

   '4. Iterate over the collection
   For Each varElement in colDemo
      Debug.Print varElement
   Next

   '5. Remove Elements from the collection
   colDemo.Remove(2)

   '6. Print the Count of elements
   Debug.Print colDemo.Count     ' Displays 2

End Sub

Note

You must recognize that the variable holding each element is declared as a variant. In For Each loops, the return type must be an object or a variant. Because this collection consists of integers, not objects, the variant type must be used.


Creating an Array of Integers

Using an array is more cumbersome than using a collection. Declaring an array, setting values to the elements, and retrieving values from the array is no more difficult than using a collection. However, when you need to add more elements than you declared (or you need to remove elements), the array becomes more difficult to use.

Listing 4.2 demonstrates all the concepts performed in the collection example in Listing 4.1. However, Listing 4.2 doesn't demonstrate how you reallocate the array to grow larger. Reallocating arrays is demonstrated after Listing 4.2.

Listing 4.2. Chap04.mdb: Comparing Arrays to Collections
Sub ArraySample()

    '1. Declare the array. The Array is set to contain 3 elements
    Dim arrIntegers(2) As Integer
    Dim intLoop As Integer
    Const RemoveElement = 0

    '2 Set positions 1 through 3
    arrIntegers(0) = 10 'Arrays are 0 based by default
    arrIntegers(1) = 20
    arrIntegers(2) = 30
    Debug.Print arrIntegers(1)

    'Estimate number of elements
    Debug.Print UBound(arrIntegers) - LBound(arrIntegers)

    ' Remove the first value from the array
    ' Move subsequent values up in the array

    For intLoop = RemoveElement To UBound(arrIntegers) - 1
         arrIntegers(intLoop) = arrIntegers(intLoop + 1)
    Next

End Sub

In this example, array limitations are apparent:

  • If you need more than three elements, you must redimension the array. To define arrays that can be redimensioned, you must declare the array as follows:

    Dim arrIntegers() As Integer
    
  • By declaring an array this way, you can redefine its size as needed by using the ReDim keyword:

    ReDim Preserve rrIntegers(2) as Integer
    

    The optional Preserve keyword preserves the data in the existing array. You can't change the data type of a resized array, and you're limited to changing only the last dimension of multidimensional arrays.

  • The example in Listing 4.2, which shows how to print the number of elements by using the UBound() function, isn't accurate because it represents only the size of the array. For example, calling that line of code after removing an element still returns 2.

  • Removing an element is inefficient if you want to keep the elements together because you would need to iterate over all subsequent elements and move them up in the order. Depending on whether the item is near the beginning of the list, you might have to move quite a few elements.

If you compare the code in Listing 4.1 with the code in Listing 4.2, you can see that collections are usually superior to arrays, but that doesn't mean you should never use an array. When you know that you need to store only a fixed number of elements, arrays might be faster and more convenient to use. However, if the number of elements you're storing varies and elements are frequently added and removed, collections almost always offer you better memory management and efficiency.

Understanding Advanced Uses of Collections

Collections have many more uses than just managing integer lists. Each element of the collection can be of a different data type. Furthermore, collections aren't limited to the basic data types, which means you can have collections of objects. In the later section “Supporting Multiple-Form Instances,” you learn how to use custom collections to manage your forms.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.198.94