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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

50 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 1<br />

Write a program that uses a file for input <strong>and</strong> produces the same output as the following code. (The form<br />

design for all examples in this section consists of a comm<strong>and</strong> button <strong>and</strong> a picture box.)<br />

Private Sub cmdDisplay_Click()<br />

Dim houseNumber As Single, street As String<br />

picAddress.Cls<br />

houseNumber = 1600<br />

street = “Pennsylvania Ave.”<br />

picAddress.Print “The White House is located at”; houseNumber; street<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

The White House is located at 1600 Pennsylvania Ave.<br />

SOLUTION:<br />

Use Windows’ Notepad to create the file DATA.TXT containing the following two lines:<br />

1600<br />

“Pennsylvania Ave.”<br />

In the following code, the fifth line looks for the first item of data, 1600, <strong>and</strong> assigns it to the numeric<br />

variable houseNumber. (<strong>Visual</strong> <strong>Basic</strong> records that this piece of data has been used.) The sixth line looks<br />

for the next available item of data, “Pennsylvania Ave.”, <strong>and</strong> assigns it to the string variable street. Note:<br />

You will have to alter the Open statement in the fourth line to tell it where the file DATA.TXT is located.<br />

For instance, if the file is in the root directory (that is, folder) of a diskette in drive A, then the line should<br />

read Open “A:\DATA.TXT” For Input As #1. If the file is located in the subdirectory (that is,<br />

folder) VB6 of the C drive, then the statement should be changed to Open ”C:\VB6\DATA.TXT”<br />

For Input As #1. See Comment 1 for another option.<br />

Private Sub cmdReadFile_Click()<br />

Dim houseNumber As Single, street As String<br />

picAddress.Cls<br />

Open “DATA.TXT” For Input As #1<br />

Input #1, houseNumber<br />

Input #1, street<br />

picAddress.Print “The White House is located at”; houseNumber; street<br />

Close #1<br />

End Sub<br />

A single Input # statement can assign values to several different variables. For instance,<br />

the two Input # statements in the solution of Example 1 can be replaced by the single statement<br />

Input #1, houseNumber, street<br />

In general, a statement of the form<br />

Input #n, var1, var2, ..., varj<br />

has the same effect as the sequence of statements<br />

Input #n, var1<br />

Input #n, var2<br />

.<br />

.<br />

.<br />

Input #n, varj

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

Saved successfully!

Ooh no, something went wrong!