Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 17: Lumping Related Data in Records 237

The following bit of code shows that you must first define your record and then create a variable to represent your record:

TYPE EmployeeRecord

FullName AS STRING * 15

Address AS STRING * 25

Phone AS STRING * 14

Salary AS SINGLE

END TYPE

DIM Workers AS EmployeeRecord

The DIM command in the preceding example tells the computer, “Create the variable Workers that can hold data that the record EmployeeRecord defines.”

Only after you define a variable to represent your record can you start stuffing data into the record.

Manipulating Data in Records

To add data to and retrieve data from a record, you need to specify the following two items:

The variable name that represents the record

The variable name inside the record in which you want to store data or from which you want to take data

Storing data in a record

To store data in a record, you need to specify both the variable name (which represents a record) and the specific record variable to use, as in the following example:

RecordVariable.Variable = Data

Suppose that you had a record definition like the following example:

TYPE BomberInfo

NickName AS STRING * 16

MissionsFlown AS INTEGER

Crew AS INTEGER

END TYPE

238 Part IV: Dealing with Data Structures

You define a variable to represent this record as follows:

DIM B17 AS BomberInfo

Then, if you want to store a string in the NickName variable, you use the following command:

B17.NickName = “Brennan’s Circus”

This command tells the computer, “Look for the variable B17 and store the string “Brennan’s Circus” in the NickName variable.”

Retrieving data from a record

You can retrieve stored data by specifying both the record’s variable name and the specific variable that contains the data that you want to retrieve, as follows:

Variable = RecordVariable.Variable

Suppose, for example, that you have the following record definition and a variable to represent that record:

TYPE BomberInfo

NickName AS STRING * 15

MissionsFlown AS INTEGER

Crew AS INTEGER

END TYPE

DIM B24 AS BomberInfo

See how C creates records

Different languages use different ways to create identical data structures. The C programming language creates a record (known as a structure in the C language) as shown in the following example:

struct bomberinfo { char nickname[15] int missionsflown int crew

} B24;

This bit of C code creates the structure (or record) bomberinfo. The bomberinfo structure can store a nickname up to 15 characters, an integer in the variable missionsflown, and another integer in the variable crew. In addition, this code also creates the variable B24 that represents this structure.