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.

14. Variable names should describe the role of the variable. Also, some programmers<br />

use a prefix, such as sng or str, to identify the type of a variable. For example,<br />

they would use names like sngInterestRate <strong>and</strong> strFirstName.<br />

2.5 INPUT AND OUTPUT<br />

So far we have relied on assignment statements to assign values to variables. Data also can be<br />

stored in files <strong>and</strong> accessed through Input # statements, or data can be supplied by the user in<br />

a text box or input box. The Print method, with a little help from commas <strong>and</strong> the Tab function,<br />

can spread out <strong>and</strong> align the display of data in a picture box or on a printer. Message<br />

boxes grab the user’s attention <strong>and</strong> display temporary messages. Comment statements allow<br />

the programmer to document all aspects of a program, including a description of the input<br />

used <strong>and</strong> the output to be produced.<br />

■ READING DATA FROM FILES<br />

In Section 1, we saw how to create data files with Windows’ Notepad. (As a rule of thumb, <strong>and</strong><br />

simply as a matter of style, we enclose each string in quotation marks.) A file can have either<br />

one item per line or many items (separated by commas) can be listed on the same line. Usually,<br />

related items are grouped together on a line. For instance, if a file consisted of payroll information,<br />

each line would contain the name of a person, that person’s hourly wage, <strong>and</strong> the number<br />

of hours that person worked during the week, as shown in Figure 2-24.<br />

FIGURE 2-24 Contents of STAFF.TXT<br />

“Mike Jones”, 7.35, 35<br />

“John Smith”, 6.75, 33<br />

The items of data will be assigned to variables one at a time in the order they appear in<br />

the file. That is, “Mike Jones” will be the first value assigned to a variable. After all the items<br />

from the first line have been assigned to variables, subsequent requests for values will be<br />

read from the next line.<br />

Data stored in a file can be read in order (that is, sequentially) <strong>and</strong> assigned to variables<br />

with the following steps.<br />

1. Choose a number from 1 to 255 to be the reference number for the file.<br />

2. Execute the statement<br />

Open “filespec” For Input As #n<br />

where n is the reference number. This procedure is referred to as Opening a file<br />

for input. It establishes a communications link between the computer <strong>and</strong> the<br />

disk drive for reading data from the disk. Data then can be input from the specified<br />

file <strong>and</strong> assigned to variables in the program.<br />

3. Read items of data in order, one at a time, from the file with Input # statements.<br />

The statement<br />

Input #n, var<br />

causes the program to look in the file for the next available item of data <strong>and</strong><br />

assign it to the variable var. In the file, individual items are separated by commas<br />

or line breaks. The variable in the Input # statement should be the same<br />

type (that is, string versus numeric) as the data to be assigned to it from the file.<br />

4. After the desired items have been read from the file, close the file with the<br />

statement<br />

Close #n<br />

Input <strong>and</strong> Output 49

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

Saved successfully!

Ooh no, something went wrong!