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.

604<br />

Data Structures<br />

The array name can be any descriptive name. The array size defines how<br />

many items the array can hold. Because the array is zero-based, an array<br />

defined as a size 10 can actually hold 11 items. The data type defines the<br />

type of data the array can hold, such as all strings or all integers. To create<br />

an array that can hold 11 strings, you could define an array like this:<br />

DIM PetNames (10) AS String<br />

If you want an array to grow or shrink while your program runs, you can<br />

define a dynamic array. To define a dynamic array, omit the array size and<br />

then define the array size right be<strong>for</strong>e you start storing items into that array,<br />

such as<br />

DIM ArrayName () AS DataType<br />

REDIM ArrayName (ArraySize)<br />

The REDIM keyword tells the program to resize the array.<br />

Creating a collection and a dictionary<br />

Arrays can be too restrictive because they can only hold one data type. For a<br />

more flexible alternative, use a collection instead. Two big advantages of a<br />

collection over an array are that a collection can hold different data types<br />

and can grow and shrink without having to define its size. To define a collection,<br />

define a collection name as follows:<br />

DIM Name AS NEW COLLECTION<br />

Unlike arrays where the first item is assigned an index number of 0, a collection<br />

assigns the first item an index number of 1.<br />

A variation of a collection is a dictionary (available only in REALbasic). The<br />

two main advantages of a dictionary are that you can assign descriptive<br />

values, or keys, to each item stored in a dictionary. This makes it easier and<br />

faster to retrieve data.<br />

To retrieve an item from a dictionary, specify the key. To retrieve an item<br />

from an array or a collection, you must either know the exact position of the<br />

item you want or you must search the entire array or collection to find the<br />

data you want. As a result, searching <strong>for</strong> items in a dictionary is much faster<br />

and easier than searching <strong>for</strong> items in arrays or collections.

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

Saved successfully!

Ooh no, something went wrong!