15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

324<br />

Using Structures with Arrays<br />

Figure 1-9:<br />

A structure<br />

can hold<br />

only one<br />

group of<br />

related data,<br />

but an array<br />

of structures<br />

can hold<br />

multiple<br />

groups of<br />

related data.<br />

Structure Company<br />

Dim Name as String<br />

Dim Contact as String<br />

Dim Sales as Single<br />

End Structure<br />

Name:<br />

Contact:<br />

Sales:<br />

Name:<br />

Contact:<br />

Sales:<br />

An array of structures can store<br />

multiple groups of data.<br />

Name:<br />

Contact:<br />

Sales:<br />

A structure can group<br />

related data in one place.<br />

Name:<br />

Contact:<br />

Sales:<br />

Name:<br />

Contact:<br />

Sales:<br />

Dim Customers(3) as Company<br />

To use a structure with an array, you must first define a structure and the<br />

variables you want to store inside that structure. So if you want to store a<br />

company name, contact person, and total sales made to the company, you<br />

could define a structure like this:<br />

Structure Company<br />

Dim Name as String<br />

Dim Contact as String<br />

Dim Sales as Single<br />

End Structure<br />

Next, you can define your array, but instead of making your array hold a data<br />

type, like strings or integers, you can make your array hold your structure<br />

like this:<br />

Dim Customers(3) as Company<br />

This code creates an array, with elements numbered from 0 to 3, which<br />

holds the Company structure that you defined, as shown in Figure 1-10.<br />

0 1 2 3<br />

Figure 1-10:<br />

An array of<br />

structures<br />

acts like a<br />

Rolodex file<br />

or a simple<br />

database.<br />

BYOB Inc.<br />

Ellie Parks<br />

58654.83<br />

Array<br />

Acme Inc.<br />

Joe Dunn<br />

6457.79<br />

Structures<br />

Microsoft<br />

Bill Gates<br />

50195.27<br />

Infinity LLC<br />

Mary Young<br />

485.03

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!