Chapter 4: Using Arrays

4.1 Introduction
Why are arrays so useful? Because we don't need to use 1000 variable names to store 1000 pieces of information. Imagine the Address Book we were looking at in Chapter 2, and having to define 1000 variable names constrained to our AddressBookType.
  DIM AddressBook1 AS AddressBookType
  DIM AddressBook2 AS AddressBookType
       :
Well, you see how tedious this becomes. It would be much easier if we had arrays, luckily we do.
4.2 Defining arrays...
Let's take a look at our Address Book again:
  DIM AddressBook(1000) AS AddressBookType
Doesn't look too bad, but how do we use it?
  AddressBook(1).Address = "Unknown St."
  AddressBook(2).Address = "Unknown Ave."
Looks easy enough, and it is, arrays are so important, most applications you write will contain arrays.
4.3 Dynamic allocation
When you allocate a block of memory for arrays, that block of memory is gone, so if you had an Address Book of 1000 entries, you would allocate 1000 blocks of AddressBookType. However, do we ever know how many entries there will be? For a business firm, there's no way of knowing, so we could allocate 10000 entries, and not use half of them, therefore wasting 5000 blocks of memory. This is where dynamic allocation is so important. QBasic offers REDIM, which (in my mind) is only a partial dynamic allocation. Why only partial? Because REDIM wipes out all data that's previously been stored. PDS (or BASIC 7.1) offers REDIM PRESERVE which preserves the data. With REDIM, you can set a small maximum index, like 1000, and REDIM it if there are more entries. The whole story can be found in the help text, just type REDIM and press F1 for all the details.

Back to Contents Next: Chapter 5