Chapter 7 Notes - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 7 Notes

Description:

Chapter 7 Notes. Modified By: Evans Adams. July 2004. Chapter 7 Arrays ... is already filled when the OnClick Event Procedure executes (use Debugger to demonstrate) ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 25
Provided by: ada48
Category:
Tags: chapter | notes | onclick

less

Transcript and Presenter's Notes

Title: Chapter 7 Notes


1
Chapter 7 Notes
  • Modified By Evans Adams
  • July 2004

2
Chapter 7 Arrays
  • 7.1 Creating and Accessing Arrays
  • 7.2 Using Arrays
  • 7.3 Some Additional Types of Arrays
  • 7.4 Sorting and Searching (Omit)
  • 7.5 Two-Dimensional Arrays

3
Simple variables
  • A variable (or simple variable) is a name to
    which VB.NET can assign a single value.
  • An array variable is a collection of simple
    variables of the same type to which VB.NET can
    efficiently assign a list of values.

4
Example
  • Suppose that you want to evaluate the examgrades
    for 30 students and to display the names of the
    students whose scores are above average.
  • Private Sub btnDisplay_Click(...) Handles
    btnDisplay.ClickDim student1 As String, score1
    As DoubleDim student2 As String, score2 As
    DoubleDim student3 As String, score3 As Double
  • Dim student30 As String, score30 As Double
  • We would then need to write 60 assignment
    statements to assign values to these 60 variables
    (30 students and 30 scores)
  • The resulting program would be long and
    cumbersome

5
Defining Arrays
Upper Bound of Array Subscripts, Size of the
Array 29 1 30 Array indices (subscripts)
start with 0
  • Dim student(29) As String to hold the names
  • Dim score(29) As Double to hold the scores

Array name
Data type
6
Putting Values into an Array
  • student(0) "Tom Brown"

subscript
Read "student sub zero equals Tom Brown Which
means that the string "Tom Brown" is being stored
into position 0 in the array called student
7
Array Terminology
  • Dim arrayName(n) As DataType
  • 0 is the lower bound of the array
  • n is the upper bound of the array the last
    available subscript in this array
  • The number of elements (n1) is the size of the
    array

8
Array Conventions
  • Array elements are act like regular variables
    of the assigned data type, except that they
    require a subscript
  • Can be assigned values with assignment statements
  • Can be displayed in list boxes and text boxes
  • Default values are Nothing for Strings and 0 for
    numeric variables
  • Scope can be Class Level or Local depending upon
    where the declaration statement is placed
  • Subscripts can be either constants, variables or
    expressions, i.e.,
  • student(5), student(n), student(n12)

9
Example 7.1.1
  • Private Sub btnWhoWon_Click(...)Handles
    btnWhoWon.Click
  • Dim teamName(3) As String Define or Declare the
    Array variable
  • Dim n As Integer
  • 'Place Super Bowl Winners into the array
  • teamName(0) "Packers"
  • teamName(1) "Packers"
  • teamName(2) "Jets"
  • teamName(3) "Chiefs
  • n CInt(txtNumber.Text)
  • txtWinner.Text teamName(n-1) 'Access array
    element n 1 No need to ask user for numbers 0
    to 3
  • End Sub

10
Output Example 1
11
Example 7.1.2
  • Dim teamName(3) As String Array Name is a
    Class-Level Variable and accessible to all
    procedures
  • Private Sub btnWhoWon_Click(...)Handles
    btnWhoWon.Click
  • Dim n As Integer
  • n CInt(txtNumber.Text)
  • txtWinner.Text teamName(n)
  • End Sub
  • Private Sub Form1_Load(...) Handles MyBase.Load
  • The Load Event fires when the form is loaded
    into memory, hence the array is already filled
    when the OnClick Event Procedure executes (use
    Debugger to demonstrate)
  • teamName(0) "Packers"
  • teamName(1) "Packers"
  • teamName(2) "Jets
  • teamName(3) Chiefs
  • End Sub

12
Lab Exercise 7-1
  • Demo and discuss While Loop Load and Display
  • End of Lecture 1

13
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), value2 to arrayName(2), ..., and
    valueN to arrayName(N).
  • Example
  • Dim teamName() As String "Packers", _
  • "Packers", "Jets", "Chiefs"

14
ReDim Statement
  • The size of an array may be changed after it is
    created
  • ReDim arrayName(m)
  • where arrayName is the name of the already
    declared array and m is an Integer
    literal,variable, or expression.

15
Preserve
  • To keep any data that has already been stored in
    the array when resizing it, use
  • ReDim Preserve arrayName(m)

16
Example 7.1.3 (Beginning of FormLoad Event Proc.)
Dim teamName() As String Class Level Array
to hold team names Dim upperBound As Integer
Number of Super Bowls Played Private Sub
Form1_Load () Handles MyBase.Load When the
Form Loads, Read the file, load the array of team
names from a file and set properties of
certain controls Dim sr As IO.StreamReader
IO.File.OpenText("SBWINNERS.TXT") Dim name As
String Dim numLines As Integer 'Count
the number of lines in the file and assign it to
upperBound numLines 0 Do While (sr.Peek
ltgt -1) name sr.ReadLine numLines
1 Loop upperBound numLines - 1
sr.Close()
17
Example 7.1.3 (Rest of FormLoad Event Proc.)
'Specify the title bar of the form Me.Text
"First " upperBound 1 " Super Bowls
'Specify the caption of the Next Winner button
btnNextWinner.Text "Add Winner of Game "
upperBound 2 'Specify the size of the array
and fill it with the entries of the file
ReDim teamName(upperBound) re-create Class Level
array of correct size sr
IO.File.OpenText("SBWINNERS.TXT") Open the file
again to read
the team names into the
teamName array For i as Integer 0 To
upperBound can Dim the loop control variable
in the For statement teamName(i)
sr.ReadLine Store a team name into an array
element Next sr.Close() End Sub
18
Example 7.1.3 (btnDisplay Event Proc.)
Private Sub btnDisplay_Click () Handles
btnDisplay.Click 'Display the numbers of the
games won by the team in the text box Dim i
As Integer lstGamesWon.Items.Clear() For
i 0 To upperBound If teamName(i)
txtName.Text Then Add the subscript of the
element
with a matching team name in the array to the
list box lstGamesWon.Items.Add(i1) to
convert from array subscript numbers to
numbers that make sense to the user End If
Next End Sub
19
Example 7.1.3 (add Winner(s) of latest Super
Bowl(s)
Private Sub btnNextWinner_Click () Handles
btnNextWinner.Click 'Add winner(s) of next
Super Bowl(s) to the array Dim prompt As
String upperBound 1 increment last array
elements subscript 'Add one more element to
the array and preserve its contents ReDim
Preserve teamName(upperBound) 'Request the
name of the next winner prompt "Enter
winner of game " upperBound 1
teamName(upperBound) InputBox(prompt, , "")
'Update the title bar of the form and the
caption of the button Me.Text "First "
upperBound 1 " Super Bowls"
btnNextWinner.Text "Add Winner of Game "
upperBound 2 Note only the array is
changed, the file is not. We will learn how to
add data to files in Chapter 8 End Sub
20
Example 7.1.4 First part of event proc.
  • Private Sub btnAnalyze_Click(...) Handles
    btnAnalyze.Click
  • 'Count occurrences of the various letters in a
    sentence
  • Dim index, letterNum As Integer
  • Dim sentence, letter As String
  • Dim charCount(25) As Integer
  • 'Examine and tally each letter of the sentence
  • sentence (txtSentence.Text).ToUpper convert
    to upper case
  • For letterNum 1 To sentence.Length
  • letter sentence.Substring(letterNum - 1,
    1)select next
  • letter of
    the sentence
  • If (letter gt "A") And (letter lt "Z") Then
  • index Asc(letter) - 65 'The ASCII value
    of "A" is 65
  • charCount(index) 1 increment counter
    for the letter
  • End If
  • Next

21
Example 7.1.4 Last part of event proc.
'List the tally for each letter of alphabet
lstCount.Items.Clear() For index 0 To 25
letter Chr(index 65) Generate each letter
from its ASCII value If charCount(index) gt
0 Then lstCount.Items.Add(letter " "
charCount(index)) End If Next End Sub
22
Out of Bounds Error
  • The following sets up an array, and then
    references an element that doesn't exist. This
    will cause an error.
  • Dim trees() As String "", "Sequoia", _
  • "Redwood", "Spruce"
  • txtBox.Text trees(5)

23
Copying 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(). It will have the same size and
    contain the same information.
  • Can use Erase arrayName after an array is no
    longer needed to release all memory cells
    allocated to the array

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com