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.

FIGURE 6-5 Flowchart for a Search of an Ordered Array<br />

■ USING PART OF AN ARRAY<br />

In some programs, we must dimension an array before knowing how many pieces of data are<br />

to be placed into it. In these cases, we dimension the array large enough to h<strong>and</strong>le all reasonable<br />

contingencies. For instance, if the array is to hold exam grades, <strong>and</strong> classes sizes vary from<br />

5 to 100 students, we use a statement such as Dim grades(1 To 100) As Integer. In such situations<br />

we must employ a counter variable to keep track of the number of values actually<br />

stored in the array. We create this counter variable using a Dim statement in the (Declarations)<br />

section of (General) so that all procedures will have access to it.<br />

EXAMPLE 2<br />

The following program requests a list of companies <strong>and</strong> then displays them along with a count.<br />

‘Demonstrate using only part of an array<br />

Dim stock(1 To 100) As String<br />

Dim counter As Integer<br />

Private Sub cmdResult_Click()<br />

If (counter < 100) Then<br />

counter = counter + 1<br />

stock(counter) = txtCompany.Text<br />

txtCompany.Text = “”<br />

txtCompany.SetFocus<br />

Else<br />

MsgBox “No space to record additional companies.”, , “”<br />

txtCompany.Text = “”<br />

cmdSummarize.SetFocus<br />

End If<br />

End Sub<br />

Using Arrays 173

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

Saved successfully!

Ooh no, something went wrong!