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.

’Determine amount of data to be processed<br />

numStudents = 0<br />

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

Do While Not EOF(1)<br />

Input #1, nTemp, sTemp<br />

numStudents = numStudents + 1<br />

Loop<br />

Close #1<br />

‘Create arrays for names <strong>and</strong> scores<br />

ReDim nom(1 To numStudents) As String, score(1 To numStudents) As Integer<br />

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

For student = 1 To numStudents<br />

Input #1, nom(student), score(student)<br />

Next student Close #1<br />

‘Analyze exam scores<br />

total = 0<br />

For student = 1 To numStudents<br />

total = total + score(student)<br />

Next student<br />

average = total / numStudents<br />

‘Display all names with above-average grades<br />

picTopStudents.Cls<br />

For student = 1 To numStudents<br />

If score(student) > average Then<br />

picTopStudents.Print nom(student)<br />

End If<br />

Next student<br />

End Sub<br />

An alternative approach to program flexibility that does not require reading the data file twice<br />

is to require that the data file begin with a line that holds the number of records to be processed.<br />

If SCORES.TXT is modified by adding a new first line that gives the number of students,<br />

then the fourth through eighteenth lines of Example 4 can be replaced with<br />

’Create arrays for names <strong>and</strong> scores<br />

Open “SCORES.TXT” For<br />

Input As #1<br />

Input #1, numStudents<br />

ReDim nom(1 To numStudents) As String, score(1 To numStudents) As Integer<br />

For student = 1 To numStudents<br />

Input #1, nom(student), score(student)<br />

Next student<br />

Close #1<br />

In Example 4, the ReDim statement allowed us to create arrays whose size was not<br />

known before the program was run. On the other h<strong>and</strong>, the arrays that were created were local<br />

to the event procedure cmdShow_Click. Many applications require form-level arrays whose<br />

size is not known in advance. Unfortunately, Dim statements cannot use variables or expressions<br />

to specify the subscript range. The solution offered by <strong>Visual</strong> <strong>Basic</strong> is to allow the<br />

(Declarations) section of (General) to contain Dim statements of the form<br />

Dim arrayName() As varType<br />

Creating <strong>and</strong> Accessing Arrays 167<br />

where no range for the subscripts of the array is specified. An array created in this manner will<br />

be form-level but cannot be used until a ReDim statement is executed in a procedure to establish<br />

the range of subscripts. The “As varType” clause can be omitted from the ReDim statement.

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

Saved successfully!

Ooh no, something went wrong!