15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

314<br />

Using an Array<br />

Using an Array<br />

The problem with a single variable is that it can hold only a single chunk of<br />

data. So if you wanted to store a name, you could create a variable, such as<br />

Dim Name as String<br />

If you wanted to store a second name, you’d have to create a second variable,<br />

such as<br />

Dim Name as String<br />

Dim Name2 as String<br />

The more names you want to store, the more separate variables you need to<br />

create. Because creating separate variables to store similar types of in<strong>for</strong>mation<br />

can get tedious, computer scientists have created a “super” variable —<br />

an array. Unlike an ordinary variable that can hold only one chunk of data,<br />

an array can hold multiple chunks of data.<br />

To create an array, you need to define these three items:<br />

✦ A variable name<br />

✦ The number of items you want to store (the array size)<br />

✦ The type of data to store (such as integers or strings)<br />

So if you wanted to store 15 names in a variable, you could create a name<br />

array, such as<br />

Dim NameArray(15) as String<br />

The preceding code tells the computer to create a NameArray array, which<br />

can hold up to 15 strings, as shown in Figure 1-2.<br />

Figure 1-2:<br />

An array<br />

can hold<br />

multiple<br />

chunks of<br />

data.<br />

John Doe Gary Wilkins Holly Creamer Bobby Jones<br />

Defining the size<br />

An array acts like a bunch of buckets (dubbed elements) that can hold<br />

exactly one item. When you create an array, you must first define the size of

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

Saved successfully!

Ooh no, something went wrong!