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

Chapter 17: Lumping Related Data in Records 239

If you’re already storing data in the B24 record, you can retrieve it by using the following commands:

GetName = B24.NickName

GetHistory = B24.MissionsFlown

GetCrew = B24.Crew

Using Records with Arrays

Although a record can store several related chunks of data under a single variable name, records by themselves aren’t very good at storing lists of related data, such as a list of names, addresses, and phone numbers. To solve this problem, you can create an array that contains a record by using the following command:

DIM ArrayName(Number) AS RecordType

Refer to Chapter 16 for more information about arrays.

Because an array is nothing more than a list representing a specific data type, you can define an array to represent a record as follows:

TYPE GulliblePeople

FullName AS STRING * 15

CashAvailable AS SINGLE

END TYPE

DIM PotentialVictims(3) AS GulliblePeople

This chunk of code tells the computer to do the following:

1.The first line creates the record GulliblePeople.

2.The second line creates a FullName string variable that can hold up to 15 characters.

3.The third line creates a CashAvailable variable that can hold a singleprecision number.

4.The fourth line marks the end of the GulliblePeople record.

5.The fifth line creates an array that can hold three records based on the GulliblePeople record. (You can refer to Figure 16-1, in Chapter 16, to see a representation of this array.)

To see a real-life example of how records work, study the following QBASIC program:

240 Part IV: Dealing with Data Structures

TYPE GulliblePeople

FullName AS STRING * 15

CashAvailable AS SINGLE

END TYPE

DIM PotentialVictims(2) AS GulliblePeople

FOR I = 1 TO 2

PRINT “Type the name of someone you want to con:”

INPUT PotentialVictims(I).FullName

PRINT “How much money do you think you can get?”

INPUT PotentialVictims(I).CashAvailable

NEXT I

PRINT

PRINT “Here is your list of future con victims:”

FOR I = 1 TO 2

PRINT PotentialVictims(I).FullName

PRINT PotentialVictims(I).CashAvailable

NEXT I

END

This program asks that you type in two names and two numbers. The first name you store in PotentialVictims(1).FullName, and the first number you store in PotentialVictims(1).CashAvailable. The second name you store in PotentialVictims(2).FullName, and the second number you store in PotentialVictims(2).CashAvailable.

Liberty BASIC doesn’t support records, so you won’t be able to get the above BASIC program to run under Liberty BASIC.