19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

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

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

3. Statements of the form<br />

picBox.Print recVar<br />

are invalid, where recVar is a record variable. Each field of a record must appear<br />

separately in a picBox.Print statement. Also, comparisons involving records<br />

using the relational operators , =, , < =, <strong>and</strong> > = are valid only with the<br />

record fields, <strong>and</strong> not with the records themselves.<br />

4. In addition to being declared as numeric, Boolean, or fixed-length string data<br />

types, the elements of a user-defined variable type can also be declared as other<br />

types of records. However, we do not use such structures in this text.<br />

5. An array of fixed-length strings is declared by a statement of the form<br />

Dim arrayName (a To b) As String * n<br />

6. An array of records would be declared with a statement such as<br />

Dim colleges(1 to 8) As collegeData<br />

<strong>and</strong> information would be accessed with statements such as<br />

picBox.Print colleges(1).nom<br />

7. When fixed-length strings are passed to <strong>and</strong> from procedures, the corresponding<br />

parameter in the Sub or Function statement must be an ordinary (variablelength)<br />

string.<br />

8. Most data types can be used as field types appearing in a Type block, including<br />

(variable-length) strings. However, the String data type is not allowed in Type<br />

blocks that will be used with r<strong>and</strong>om-access files.<br />

8.2 RANDOM-ACCESS FILES<br />

A r<strong>and</strong>om-access file is like an array of records stored on a disk. The records are numbered<br />

1, 2, 3, <strong>and</strong> so on, <strong>and</strong> can be referred to by their numbers. Therefore, a r<strong>and</strong>om-access file resembles<br />

a box of index cards, each having a numbered tab. Any card can be selected from the<br />

box without first reading every index card preceding it; similarly, any record of a r<strong>and</strong>om-access<br />

file can be read without having to read every record preceding it.<br />

One statement suffices to open a r<strong>and</strong>om-access file for all purposes: creating, appending,<br />

writing, <strong>and</strong> reading. Suppose a record type has been defined with a Type block <strong>and</strong> a<br />

record variable, called recVar, has been declared with a Dim statement. Then after the statement<br />

Open “filespec” For R<strong>and</strong>om As #n Len = Len(recVar)<br />

is executed, records may be written, read, added, <strong>and</strong> changed. The file is referred to by<br />

the number n. Each record will have as many characters as allotted to each value of recVar.<br />

Suppose appropriate Type, Dim, <strong>and</strong> Open statements have been executed. The two-step<br />

procedure for entering a record into the file is as follows.<br />

1. Assign a value to each field of a record variable.<br />

2. Place the data into record r of file #n with the statement<br />

Put #n, r, recVar<br />

where recVar is the record variable from Step 1.<br />

R<strong>and</strong>om-Access Files 251

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

Saved successfully!

Ooh no, something went wrong!