324 Using Structures with Arrays Figure 1-9: A structure can hold only one group of related data, but an array of structures can hold multiple groups of related data. Structure Company Dim Name as String Dim Contact as String Dim Sales as Single End Structure Name: Contact: Sales: Name: Contact: Sales: An array of structures can store multiple groups of data. Name: Contact: Sales: A structure can group related data in one place. Name: Contact: Sales: Name: Contact: Sales: Dim Customers(3) as Company To use a structure with an array, you must first define a structure and the variables you want to store inside that structure. So if you want to store a company name, contact person, and total sales made to the company, you could define a structure like this: Structure Company Dim Name as String Dim Contact as String Dim Sales as Single End Structure Next, you can define your array, but instead of making your array hold a data type, like strings or integers, you can make your array hold your structure like this: Dim Customers(3) as Company This code creates an array, with elements numbered from 0 to 3, which holds the Company structure that you defined, as shown in Figure 1-10. 0 1 2 3 Figure 1-10: An array of structures acts like a Rolodex file or a simple database. BYOB Inc. Ellie Parks 58654.83 Array Acme Inc. Joe Dunn 6457.79 Structures Microsoft Bill Gates 50195.27 Infinity LLC Mary Young 485.03
Drawbacks of Arrays 325 To store data in an array of structures, you need to identify the array element (in this example numbered 0 to 3) and the specific variable inside the structure to store your data. So if you wanted to store data in array element number 2, you could do the following: Customers(2).Name = “Microsoft” Customers(2).Contact = “Bill Gates” Customers(2).Sales = 50195.27 Retrieving data from an array of structures means identifying the array name, the array element followed by the variable name stored in that structure. So if you wanted to print the name stored in the Contact variable of array element number 2, you could do the following: Print Customers(2).Contact This code would print Bill Gates on-screen. Storing and retrieving data from an array of structures means identifying the following items: ✦ The array name (such as Customers) ✦ The array element number (such as 2) ✦ The variable inside the structure (such as Contact) Drawbacks of Arrays Arrays can be handy for storing lists of related data in a single location. However, arrays have several drawbacks: ✦ Large arrays take up space. ✦ Arrays can hold only one data type at a time. ✦ Searching and sorting arrays is difficult. ✦ Inserting and removing data from arrays is clumsy. Book III Chapter 1 Structures and Arrays Sizing The biggest problem with arrays is defining the size of an array ahead of time: ✦ If you know you need to store 20 names, it’s easy to define an array to hold 20 strings. ✦ If you aren’t sure if your program needs to store 20 names or 20,000 names, you have to define the largest array you think your program will ever need, so most of the array will be empty and waste memory.