Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.

Similar presentations


Presentation on theme: "Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous."— Presentation transcript:

1 Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: Arrays

2 Objectives  Creating and Accessing Arrays  Using Arrays  Some Additional Types of Arrays  Sorting and Searching  Two-Dimensional Arrays

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

4 Simple and Array Variables  Consider the following situation:  Suppose that you want to evaluate the exam grades for 30 students.  Not only do you want to compute the average score, but you also want to display the names of the students whose scores are above average. 4

5 Declaring an Array Variable  Visual Basic provides a data structure called an array that let us do what we tried to accomplish in the loop.  The variables names, similar to those in the previous example, will be: Student(0), student(1),.., student(29) AND Score(0),score(1),….,score(29)

6 Declaring an Array Variable  We refer to these collection of variables as the array variables student() and score ().  The numbers inside the parentheses of the individual variables are called subscripts or indexes and each individual variable is called a subscripted variable or element.

7 Declaring an Array Variable  Array variables have the same kinds of names as simple variables.  If arrayName is the name of an array variable and n is an integer literal, variable the the declaration statement Dim arrayName(n) As varType reserves space in memory to hold the values of the subscripted varialbes arrayName(0),….,arrayName(n).

8 Simple and Array Variables  Many variables  An array variable 8 72 72 Value1Value4Value7…… Values 0 1 2 3 4 5 6Location

9 Simple and Array Variables  Many variables  VS  An array variable  Which is better? Benefits? 9

10 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. COULD DO 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 Handles btnDisplay.Click Dim student0 As String, score0 As Double Dim student1 As String, score1 As Double Dim student2 As String, score2 As Double 10

11 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. BETTER Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click Dim student(29) As String, score(29) As Double Handles btnDisplay.Click Dim student(29) As String, score(29) As Double 11

12 Declaring an Array Variable  The subscripted variables will all have the same data type, namely, the type specified by varType.  The could be all String variables or all integer Variables.

13 Using Arrays Dim student(29) As String Dim score(29) As Double Why 29 if we need 30 students?? 13 Array name Upper bound of subscripts in the array Data type

14 Putting Values into an Array student(0) = "Tom Brown" 14 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.

15 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 15

16 Example 1: Form 16 txtNumber txtWinner

17 Example 1 Private Sub btnWhoWon_Click(...) _ Handles btnWhoWon.Click Handles btnWhoWon.Click Dim teamName(3) As String Dim teamName(3) As String Dim n As Integer Dim n As Integer 'Place Super Bowl Winners into the array 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(0) = "Packers" teamName(1) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(2) = "Jets" teamName(3) = "Chiefs" teamName(3) = "Chiefs" 'Access array 'Access array n = CInt(txtNumber.Text) n = CInt(txtNumber.Text) txtWinner.Text = teamName(n - 1) txtWinner.Text = teamName(n - 1) End Sub 17

18 Example 1: Output 18

19 Load Event Procedure  The array teamName was assigned values within the btnWhoWon_Click event procedure.  Every time the button is clicked, the values are reassigned to the array.  The manners of assigning values to an array can be very inefficient, especially in program with large arrays where the task of the program may be repeated numerous times for different user input. 19

20 Load Event Procedure Occurs as the Form loads in memory Private Sub frmName_Load(...) _ Private Sub frmName_Load(...) _ Handles MyBase.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. - This is the FIRST thing that loads - Loads BEFORE you see the form 20

21 Example 2 Dim teamName(3) As String Private Sub btnWhoWon_Click(...) Handles btnWhoWon.Click Dim n As Integer Dim n As Integer n = CInt(txtNumber.Text) n = CInt(txtNumber.Text) txtWinner.Text = teamName(n - 1) txtWinner.Text = teamName(n - 1) End Sub Private Sub frmBowl_Load(...) Handles MyBase.Load 'Place Super Bowl Winners into the array 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(0) = "Packers" teamName(1) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(2) = "Jets" teamName(3) = "Chiefs" teamName(3) = "Chiefs" End Sub 21

22 Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = {value0, _ value1, value2,..., valueN} 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). 22

23 Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = {value0, _ value1, value2,..., valueN} value1, value2,..., valueN}  For Example: Dim Students() As String = {"Jack", "John", "Julie", "Jimmy", "Janet"} 23

24 Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = IO.File.ReadAllLines(filespec)  Opens filespec, reads all lines from it, and stores it in arrayName  Each line in filespec is stored in one location of arrayName 24

25 GetUpperBound Method The value of arrayName.GetUpperBound(0) is the upper bound of arrayName(). 25

26 Example Private Sub btnWhoWon_Click(...) _ Handles btnWhoWon.Click Handles btnWhoWon.Click Dim teamName(3) As String Dim teamName(3) As String Dim n As Integer Dim n As Integer 'Place Super Bowl Winners into the array 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(0) = "Packers" teamName(1) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(2) = "Jets" teamName(3) = "Chiefs" teamName(3) = "Chiefs" 'Access array 'Access array n = CInt(txtNumber.Text) n = CInt(txtNumber.Text) txtWinner.Text = teamName(n - 1) txtWinner.Text = teamName(n - 1) End Sub 26 teamName.GetUpperBound(0)?

27 Example Dim teamName() As String = {"Packers", _ "Packers", "Jets", "Chiefs"} "Packers", "Jets", "Chiefs"} txtBox.Text = CStr(teamName.GetUpperBound(0)) Output: 3 Why 3? There are 4 elements! 27

28 Example Dim Grades() As String = {70, 75, 80, 85, 90} Grades.Average  80 Grades.Count  5 Grades.Min  70 Grades.Max  90 Grades.Sum  400 28

29 ReDim Statement The size of an array may be changed after it has been created. ReDim arrayName(m) ReDim arrayName(m) changes the upper bound of the array to m. 29

30 ReDim Statement ReDim arrayName(m) ReDim arrayName(m)  Note: Since the type cannot be changed, there is no need for an “As datatype” clause at the end of the ReDim statement. 30

31 Preserve Keyword  ReDim arrayName(m) resets all values to their default.  String values to Nothing and resets all numeric values to 0.  This can be prevented with the keyword Preserve. ReDim Preserve arrayName(m)  Resizes the array and retains as many values as possible. 31

32 Out of Bounds Error  The following code references an array element that doesn't exist. This will cause an error. 32

33 Out of Bounds Error  Using a subscript greater than the upper bound of an array is not allowed. 33

34 Assignment Statement for Arrays  If arrayOne() and arrayTwo() have been declared with the same data type, then the statement arrayOne = arrayTwo arrayOne = arrayTwo  makes arrayOne() an exact duplicate of arrayTwo().  Actually, they share the same location in memory - What does this mean?? - What does this mean?? 34

35 Using Part of An Array  In some programs we must dimension an array before knowing how many pieces of data are to be placed into it.  We dimension the array large enough to handle all reasonable contingencies.

36 Using Part of An Array  If an array is to hold exam grades, and class size are at most 100 student.  We use the statement such as Dim grades (99) As Integer.  We must to employ a counter variable to keep track of the number of values actually stored in the array.

37 Two-Dimensional Arrays  Each array discussed so far held a single list of items. Such array variables are called one- dimensional Array.  An array can also hold the contents of table with several rows and columns. Such array variables are called two-dimensional arrays. 37

38 Two-Dimensional Arrays  They have the same types of names as other array variables.  The only difference is that they have two subscripts, each with its own upper bound.  The firs subscript is determined by the number of rows in the table, and the second subscript is determined by the number of colummns.  Dim arrayName (m,n) As DataType 38

39 Two-Dimensional Arrays 1234 BusinessHarvardStanfordU of PAMIT EngineeringMITStanfordUC BerkGA Teach LawYaleHarvardStandardNYU 39 Univ(0,0) = “Harvard” Univ (1,2) = “UC Berk” Unive (2,3) = “NYU” Univ(1,1) = ? Univ(1,3) = ? Univ (3,3) = ? Univ (2,1) = ?

40 Two-Dimensional Arrays  An unsized two-dimensional array can be declared with a statement of the for  Dim arrayName(,) As VarType  Two dimension array can be declared and initialized at the same time wit a statement of the form  Dim arrayName(,) As varType = {{ROW0}, {ROW1}, … {ROWn}} 40

41 Erasing an Array  An array can consume a large block of memory.  After the array is no longer needed, we can release all memory allocated to the array by executing the following statement: Erase arrayName 41


Download ppt "Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous."

Similar presentations


Ads by Google