Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.

Similar presentations


Presentation on theme: "Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays."— Presentation transcript:

1 Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays

2 Chapter 72 Variable vs Array

3 Chapter 73 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.

4 Chapter 74 Creating and Accessing Arrays Declaring an Array Variable The Load Event Procedure The GetUpperBound Method ReDim Statement Using an Array as a Frequency Table

5 Chapter 75 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 student1 As String, score1 As Double Dim student2 As String, score2 As Double Dim student3 As String, score3 As Double

6 Chapter 76 It’s better when Using Arrays Dim student(29) As String Dim score(29) As Double Array name Upper bound of subscripts in the array Data type

7 Chapter 77 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 at the first location in the array called student… because all arrays begin counting at 0.

8 Chapter 78 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, n + 1, is the size of the array

9 Chapter 79 Upper Bound & Lower Bound This is Upper Bond This is Lower Bond Number of elements in this array is

10 Chapter 710 Lab sheet 7.1: Form mtxtNumber txtWinner

11 Chapter 711 Example – Initialize the Array in the Button 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

12 Chapter 712 Lab sheet 7.1: Early Super Bowls

13 Chapter 713 Load Event Procedure Occurs as the Form loads in memory Private Sub frmName_Load(...) _ Handles MyBase.Load The keyword MyBase refers to the form being loaded. This event procedure is a good place to assign values to an array.

14 Chapter 714 Example- Initialize the Array in Form Load 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 Private Sub frmBowl_Load(...) Handles MyBase.Load 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(3) = "Chiefs" End Sub

15 Chapter 715 Initializing Arrays when they are being created 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).


Download ppt "Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays."

Similar presentations


Ads by Google