Page 177 - Open Soource Technologies 304.indd
P. 177
Unit 7: Arrays in Visual Basic
Me.Text = “First “ & upperBound + 1 & “ Super Bowls”
btnNextWinner.Text = “Add Winner of Game “ & upperBound + 2
End Sub
[Run, type “49ers” into the text box, and press the Display button. Then, feel free to add
subsequent winners. Your additions will be taken into account when you next press the Display
button.]
The ReDim statement can appear only at procedure level. This means you can
redefine arrays inside a procedure but not at class or module level.
7.2.1 Using an Array as a Frequency
An array can be used as either a checklist or a frequency table, as in the next example.
2: The following program requests a sentence as input and records the
frequencies of the letters occurring in the sentence. The first element of the
array charCount() holds the frequency with which the letter A occurs in the
sentence, and so on. Recall that the function Asc associates each character
with its position in the ANSI table.
Private Sub btnAnalyze_Click(...) Handles btnAnalyze.Click
‘Count occurrences of the various letters in a sentence
Dim index As Integer
Dim sentence, letter As String
Dim charCount(25) As Integer
LOVELY PROFESSIONAL UNIVERSITY 171