CH 07
CH 07
Chapter 7 - VB 2008 by
Schneider 1
7.1 Creating and Accessing
Arrays
• Declaring an Array Variable
• The Load Event Procedure
• The GetUpperBound Method
• ReDim Statement
• Using an Array as a Frequency Table
• Assignment Statement for Arrays
• User-Defined Array-Valued Functions
Chapter 7 - VB 2008 by
Schneider 2
Simple and Array Variables
• A variable (or simple variable) is a
name to which Visual Basic can assign
a single value.
• An array variable is a collection of
simple variables of the same type to
which Visual Basic can efficiently
assign a list of values.
Chapter 7 - VB 2008 by
Schneider 3
Example
• Suppose that you want to evaluate the exam
grades for 30 students and to display the
names of the students whose scores are
above average.
Private Sub btnDisplay_Click(...) _
Handles btnDisplay.Click
Dim student0 As String, score0 As Double
Dim student1 As String, score1 As Double
Dim student2 As String, score2 As Double
Chapter 7 - VB 2008 by
Schneider 4
Using Arrays
Upper bound of subscripts
in the array
Chapter 7 - VB 2008 by
Schneider 5
Putting Values into an Array
student(0) = "Tom Brown"
subscript
Chapter 7 - VB 2008 by
Schneider 7
Example 1: Form
mtxtNumber
txtWinner
Chapter 7 - VB 2008 by
Schneider 8
Example 1
Private Sub btnWhoWon_Click(...) _
Handles btnWhoWon.Click
Dim teamName(3) As String
Dim n As Integer
'Place Super Bowl Winners into the array
teamName(0) = "Packers"
teamName(1) = "Packers"
teamName(2) = "Jets"
teamName(3) = "Chiefs"
'Access array
n = CInt(txtNumber.Text)
txtWinner.Text = teamName(n - 1)
End Sub
Chapter 7 - VB 2008 by
Schneider 9
Example 1: Output
Chapter 7 - VB 2008 by
Schneider 10
Load Event Procedure
Occurs as the Form loads in memory
Private Sub frmName_Load(...) _
Handles MyBase.Load
Chapter 7 - VB 2008 by
Schneider 11
Example 2
Dim teamName(3) As String
Private Sub btnWhoWon_Click(...) Handles btnWhoWon.Click
Dim n As Integer
n = CInt(txtNumber.Text)
txtWinner.Text = teamName(n - 1)
End Sub
Chapter 7 - VB 2008 by
Schneider 12
Initializing Arrays
• Arrays may be initialized when they are created:
Dim arrayName() As varType = {value0, _
value1, value2, ..., valueN}
• declares an array having upper bound N and
assigns value0 to arrayName(0), value1 to
arrayName(1), ..., and valueN to arrayName(N).
Chapter 7 - VB 2008 by
Schneider 13
GetUpperBound Method
The value of
arrayName.GetUpperBound(0)
Chapter 7 - VB 2008 by
Schneider 14
Example
Output: 3
Chapter 7 - VB 2008 by
Schneider 15
ReDim Statement
The size of an array may be changed after
it has been created.
ReDim arrayName(m)
where arrayName is the name of the
already declared array and m is an Integer
literal, variable, or expression, changes the
upper bound of the array to m.
Chapter 7 - VB 2008 by
Schneider 16
Preserve Keyword
ReDim arrayName(m) resets all values
to their default. This can be prevented
with the keyword Preserve.
ReDim Preserve arrayName(m)
resizes the array and retains as many
values as possible.
Chapter 7 - VB 2008 by
Schneider 17
Example 4: Using an Array as a
Frequency Table
Chapter 7 - VB 2008 by
Schneider 18
Example 4: Code
Private Sub btnAnalyze_Click(...) Handles btnAnalyze.Click
'Count occurrences of the various letters in a sentence
Dim sentence, letter As String
Dim index, charCount(25) As Integer
'Examine and tally each letter of the sentence
sentence = (txtSentence.Text).ToUpper
For letterNum As Integer = 1 To sentence.Length
letter = sentence.Substring(letterNum - 1, 1)
If (letter >= "A") And (letter <= "Z") Then
index = Asc(letter) - 65 'The ANSI value of "A" is 65
charCount(index) += 1
End If
Next
Chapter 7 - VB 2008 by
Schneider 19
Example 4: Code Continued
'List the tally for each letter of alphabet
lstCount.Items.Clear()
For i As Integer = 0 To 25
letter = Chr(i + 65)
If charCount(i) > 0 Then
lstCount.Items.Add(letter & " " & _
charCount(i))
End If
Next
End Sub
Chapter 7 - VB 2008 by
Schneider 20
Example 4 Output
Chapter 7 - VB 2008 by
Schneider 21
Out of Bounds Error
The following code references an array element
that doesn't exist. This will cause an error.
Chapter 7 - VB 2008 by
Schneider 22
Assignment Statement for
Arrays
If arrayOne() and arrayTwo() have been declared
with the same data type, then the statement
arrayOne = arrayTwo
makes arrayOne() an exact duplicate of
arrayTwo(). Actually, they share the same
location in memory,
Chapter 7 - VB 2008 by
Schneider 23
7.2 Using Arrays
• Ordered Arrays
• Using Part of an Array
• Merging Two Ordered Arrays
• Passing Arrays to Procedures
Chapter 7 - VB 2008 by
Schneider 24
Ordered Arrays
An array has ascending order if
[each element] ≤ [next element].
An array has descending order if
[each element] ≥ [next element].
An array is ordered if it has ascending or
descending order.
Chapter 7 - VB 2008 by
Schneider 25
Searching Ordered Arrays
Ordered arrays can be searched more
efficiently than unordered arrays. For
instance, when searching an array
having ascending order, you can
terminate the search when you find an
element whose value is ≥ the sought-
after value.
Chapter 7 - VB 2008 by
Schneider 26
Example 1: Task
Given a name input by the user, determine
if it is in an increasing list of ten names,
Chapter 7 - VB 2008 by
Schneider 27
Flowchart for a Search of an
Increasing Array
Chapter 7 - VB 2008 by
Schneider 28
Example 1: Code
Dim nom() As String = {"AL", "BOB", "CARL", "DON", "ERIC", _
"FRED", "GREG", "HERB", "IRA", "JACK"}
Chapter 7 - VB 2008 by
Schneider 30
Passing Arrays to Procedures
• An array declared in a procedure is local to
that procedure
• An entire array can be passed to a Sub or
Function procedure
• The Call statement uses the name of the array
without parentheses.
• The header of the Sub of Function procedure
uses the name with empty set of parentheses.
Chapter 7 - VB 2008 by
Schneider 36
Example 4
• This example uses a Function procedure
to add up the numbers in an array. The
GetUpperBound method is used to
determine how many numbers are in the
array.
Chapter 7 - VB 2008 by
Schneider 37
Example 4
Private Sub btnCompute_Click(...) Handles btnCompute.Click
Dim score() As Integer = {85, 92, 75, 68, 84, 86, _
94, 74, 79, 88}
txtAverage.Text = CStr(Sum(score) / 10)
End Sub
Chapter 7 - VB 2008 by
Schneider 38
Sequential Search
• Searching successive elements of an
ordered list beginning with the first
element is called a sequential search.
Chapter 7 - VB 2008 by
Schneider 39
Passing an Array Element
• A single element of an array can be passed to a
procedure just like any ordinary numeric or string
variable.
Private Sub btnDisplay_Click(...) Handles _
btnDisplay.Click
Dim num(20) As Integer
num(5) = 10
lstOutput.Items.Add(Triple(num(5)))
End Sub
Private Function Triple(ByVal x As Integer) As Integer
Return 3 * x
End Function
Chapter 7 - VB 2008 by
Schneider 40
7.5 Two Dimensional Arrays
• One-dimensional arrays store a list of items of the
same type
• Two-dimensional arrays store a table of items of the
same type.
• Consider the rows of the table as numbered 0, 1, 2, ,,,
m and the columns numbered 0, 1, 2, …, n. Then the
array is declared with the statement
Dim arrayName(m, n) As DataType
and the item in the ith row, jth column is denoted
arrayName(i,j)
Chapter 7 - VB 2008 by
Schneider 69
Road-Mileage Table
Chicago LA NY Philly
Chapter 7 - VB 2008 by
Schneider 71
Notes on Two-Dimensional
Arrays
An unsized two-dimensional array can be
declared with a statement of the form
Dim arrayName(,) As varType
and a two-dimensional array can be
declared and initialized at the same time
with a statement of the form
Dim arrayName(,) As varType =
{{ROW0}, {ROW1},... {ROWm}}.
Dim score(,) As Integer = {{1, 2, 3}, {4, 5, 6}}
Chapter 7 - VB 2008 by
Schneider 72
ReDim and Two-Dimensional
Arrays
• An already-created array can be resized with
ReDim arrayName(r, s)
• which loses the current contents, or with
ReDim Preserve arrayName(r, s)
• When Preserve is used, only the column can be
resized.
• ReDim cannot change the number of dimensions
in an array.
Chapter 7 - VB 2008 by
Schneider 73