Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic 2010 How to Program

Similar presentations


Presentation on theme: "Visual Basic 2010 How to Program"— Presentation transcript:

1 Visual Basic 2010 How to Program
Arrays Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.

2 7.2  Arrays An array is a group of variables (called elements) containing values that all have the same type. To refer to a particular element in an array, we specify the name of the array and the position number of the element to which we refer. © by Pearson Education, Inc. All Rights Reserved.

3 7.2 Arrays Accessing Array Elements
The position number in parentheses more formally is called an index or “subscript.” An index must be a nonnegative integer or integer expression. If a program uses an expression as an index, the expression is evaluated first to determine the index. For example, if variable value1 is equal to 5, and variable value2 is equal to 6, then the statement c(value1 + value2) += 2 adds 2 to array element c(11). © by Pearson Education, Inc. All Rights Reserved.

4 7.2  Arrays Array Length Every array “knows” its own length (that is, number of elements), which is determined by the array’s Length property, as in: c.Length All arrays have the methods and properties of class System.Array. © by Pearson Education, Inc. All Rights Reserved.

5 7.3 Declaring and Allocating Arrays
The following statement can be used to declare the array : Dim c(11) As Integer The parentheses that follow the variable name indicate that c is an array. The number in parentheses in the array declaration helps the compiler allocate memory for the array c. © by Pearson Education, Inc. All Rights Reserved.

6 7.3 Declaring and Allocating Arrays
We also can explicitly specify the array bounds, as in Dim c(0 To 11) As Integer The explicit array bounds specified in the preceding statement indicate that the lower bound of the array is 0 and the upper bound is 11. The size of the array is still 12. Because the lower bound must always be 0, we do not include “0 To” when declaring an array’s bounds in this book. © by Pearson Education, Inc. All Rights Reserved.

7 7.3 Declaring and Allocating Arrays
Initializer Lists You can follow an array declaration with an equal sign and an initializer list in braces ({ and }) to specify the initial values of the array’s elements. For instance, Dim numbers() As Integer = {1, 2, 3, 6} declares and allocates an array containing four Integer values. © by Pearson Education, Inc. All Rights Reserved.

8 7.3 Declaring and Allocating Arrays
The compiler determines the array bounds from the number of elements in the initializer list—you cannot specify the upper bound of the array when an initializer list is present. Visual Basic can also use local type inference to determine the type of an array with an initializer list. So, the preceding declaration can be written as: Dim numbers = {1, 2, 3, 6} © by Pearson Education, Inc. All Rights Reserved.

9 7.3 Declaring and Allocating Arrays
Default Initialization When you do not provide an initializer list, the elements in the array are initialized to the default value for the array’s type—0 for numeric primitive data-type variables, False for Boolean variables and Nothing for String and other class types. © by Pearson Education, Inc. All Rights Reserved.

10 7.4 Declaring and Allocating Arrays
Array method GetUpperBound returns the index of the last element in the array. The value returned by method GetUpperBound is one less than the value of the array’s Length property. For arrays that represent lists of values, the argument passed to GetUpperBound is 0. In this example, array1.GetUpperBound(0) returns 4, which is then used to specify the upper bound of array2, so array1 and array2 have the same upper bound (4) and the same length (5). This makes it easy to display the arrays’ contents side-by- side. © by Pearson Education, Inc. All Rights Reserved.

11 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

13 7.6 Assignment - Using Arrays to Analyze Survey Results
Consider the following problem statement: Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses in an integer array and determine the frequency of each rating. Summarize the number of responses of each type (that is, 1–5). © by Pearson Education, Inc. All Rights Reserved.

14 7.8 Case Study: Flag Quiz Consider the following problem statement:
A geography instructor would like to quiz students on their knowledge of the flags of various countries. The instructor has asked you to write an application that displays a flag and allows the student to select the corresponding country from a list. The application should inform the user of whether the answer is correct and display the next flag. The application should display a flag randomly chosen from those of Australia, Brazil, China, Italy, Russia, South Africa, Spain and the United States. When the application executes, a given flag should be displayed only once. © by Pearson Education, Inc. All Rights Reserved.

15 7.8  Case Study: Flag Quiz The application uses arrays to store information. One array stores the country names. Another stores Boolean values that help us determine whether a particular country’s flag has already been displayed, so that no flag is displayed more than once during a quiz. © by Pearson Education, Inc. All Rights Reserved.

16 7.8 Case Study: Flag Quiz Flag Quiz GUI
Figure 7.6 shows the application’s GUI. The flag images should be added to the project as image resources—you learned how to do this. The images are located in the Images folder with this chapter’s examples. This application introduces the ComboBox control, which presents options in a drop-down list that opens when you click the down arrow at the right side of the control. A ComboBox combines features of a TextBox and a ListBox. © by Pearson Education, Inc. All Rights Reserved.

17 7.8  Case Study: Flag Quiz You can click the down arrow to display a list of predefined items. If you choose an item from this list, that item is displayed in the ComboBox. If the list contains more items than the drop-down list can display at one time, a vertical scrollbar appears. You may also type into the ComboBox control to locate an item. In this application, the user selects an answer from the ComboBox, then clicks the Submit Button to check if the selected country name is correct for the currently displayed flag. © by Pearson Education, Inc. All Rights Reserved.

18 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

19 7.8  Case Study: Flag Quiz ComboBox property DropDownStyle determines the ComboBox’s appearance. Value DropDownList specifies that the ComboBox is not editable (you cannot type text in its TextBox). In this ComboBox style, if you press the key that corresponds to the first letter of an item in the ComboBox, that item is selected and displayed in the ComboBox. If multiple items start with the same letter, pressing the key repeatedly cycles through the corresponding items. Set the ComboBox’s DropDownStyle property to DropDownList. © by Pearson Education, Inc. All Rights Reserved.

20 7.8  Case Study: Flag Quiz Then, set the ComboBox’s MaxDropDownItems property to 4, so that the drop-down list displays a maximum of four items at one time. We do this to demonstrate that a vertical scrollbar is added to the drop-down list to allow users to scroll through the remaining items. We show how to programmatically add items to the ComboBox shortly. Figure 7.7 shows the application’s code. © by Pearson Education, Inc. All Rights Reserved.

21 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

22 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

23 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

24 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

25 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

26 7.9 Passing an Array to a Method
To pass an array argument to a method, specify the name of the array without using parentheses. For example, if array hourlyTemperatures has been declared as Dim hourlyTemperatures(24) As Integer the method call DisplayDayData(hourlyTemperatures) passes array hourlyTemperatures to method DisplayDayData. © by Pearson Education, Inc. All Rights Reserved.

27 7.10 For Each…Next Repetition Statement
The For Each…Next repetition statement iterates through the values in a data structure, such as an array, without using a loop counter. For Each…Next behaves like a For…Next statement that iterates through the range of indices from 0 to the value returned by GetUpperBound(0). Instead of a counter, For Each…Next uses a variable to represent the value of each element. © by Pearson Education, Inc. All Rights Reserved.

28 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

29 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

30 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

31 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

32 Sorting an Array © by Pearson Education, Inc. All Rights Reserved.

33 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

34 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

35 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

36 7.11 Sorting an Array with Method Sort of Class Array
Sorting in Descending Order To sort an array in descending order, first call method Sort to sort the array, then call Array.Reverse with the array as an argument to reverse the order of the elements in the array. Exercise : asks you to modify this program to display an array’s values in both ascending and descending order. © by Pearson Education, Inc. All Rights Reserved.

37 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

38 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

39 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

40 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

41 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

42 7.14 Rectangular Arrays Declaring and Initializing Rectangular Arrays
A two-dimensional rectangular array numbers with two rows and two columns can be declared and initialized with ' numbers in a 2 by 2 array Dim numbers(1, 1) As Integer numbers(0, 0) = 1 ' leftmost element in row 0 numbers(0, 1) = 2 ' rightmost element in row 0 numbers(1, 0) = 3 ' leftmost element in row 1 numbers(1, 1) = 4 ' rightmost element in row 1 Alternatively, the initialization can be written on one line, as shown with and without local type inference below: Dim numbers = {{1, 2}, {3, 4}} Dim numbers(,) As Integer = {{1, 2}, {3, 4}} © by Pearson Education, Inc. All Rights Reserved.

43 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

44 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

45 7.15 Case Study: Maintaining Grades Using a Rectangular Array
Consider the following problem statement: An instructor gives three tests to a class of 10 students. The grades on these tests are integers in the range from 0 to 100. The instructor has asked you to develop an application to keep track of each student’s average and the class average. The instructor has also asked that there be a choice to view the grades as either numbers or letters. Letter grades should be calculated according to the following grading system: 90–100A 80–89B 70–79C 60–69D Below 60F The application should allow a user to input each student’s three test grades, then compute each student’s average and the class average for all grades entered so far. The application should display numeric grades by default. It should also display a grade distribution chart showing how many of the numeric grades fall into the ranges 0–9, 10–19, … 90–99 and 100. © by Pearson Education, Inc. All Rights Reserved.

46 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

47 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

48 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

49 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

50 7.15 Case Study: Maintaining Grades Using a Rectangular Array
GUI for the Grade Report Application In this application, we introduce RadioButton controls to allow the user to select between displaying grades in numeric or letter format. A RadioButton is a small white circle that either is blank or contains a small dot. When a RadioButton is selected, a dot appears in the circle. A RadioButton is known as a state button because it can be in only the “on” (True) state or the “off” (False) state. © by Pearson Education, Inc. All Rights Reserved.

51 7.15 Case Study: Maintaining Grades Using a Rectangular Array
You’ve previously used the CheckBox state buttons (introduced in Chapter 5). RadioButtons are similar to CheckBoxes, but RadioButtons normally appear as a group—only one RadioButton in the group can be selected at a time. By default, all RadioButtons become part of the same group. To separate them into several groups, each group must be in a different container (typically a GroupBox). © by Pearson Education, Inc. All Rights Reserved.

52 7.15 Case Study: Maintaining Grades Using a Rectangular Array
If the RadioButton is checked, the Checked property returns True; otherwise, it returns False. A RadioButton also generates an event when its checked state changes. Event CheckedChanged occurs when a RadioButton is either selected or deselected. We use this event to switch between the numeric grade and letter grade views in the gradesListBox. © by Pearson Education, Inc. All Rights Reserved.

53 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

54 7.15 Case Study: Maintaining Grades Using a Rectangular Array
The rectangular array grades (line 4) has 10 rows and 3 columns to store the grades for 10 students and three exams per student. Variable studentCount (line 5) keeps track of the number of students processed so far. After 10 students are processed, the program disables the input TextBoxes and Submit Button. The GradeReport_Load event handler (lines 8–13) displays the column heads in the gradesListBox. © by Pearson Education, Inc. All Rights Reserved.

55 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

56 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

57 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

58 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

59 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

60 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

61 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

62 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

63 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

64 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

65 7.15 Case Study: Maintaining Grades Using a Rectangular Array
Method DisplayBarChart Many programs present data to users in a visual or graphical format. For example, numeric values are often displayed as bars in a bar chart. In such a chart, longer bars represent proportionally larger numeric values (see Fig. 7.18). Method DisplayBarChart (Fig. 7.27) displays a graphical summary of the grade distribution by creating a bar chart that shows how many numeric grades fall into each of the ranges 0–9, 10–19, … 90–99 and 100. © by Pearson Education, Inc. All Rights Reserved.

66 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

67 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

68 7.16 Resizing an Array with the ReDim Statement
An array’s size cannot be changed, so a new array must be created if you need to change the size of an existing array. The ReDim statement “resizes” an array at execution time by creating a new array and assigning it to the specified array variable. Figure 7.28 demonstrates the ReDim statement. © by Pearson Education, Inc. All Rights Reserved.

69 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

70 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

71 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

72 7.16 Resizing an Array with the ReDim Statement
The output of Fig. 7.28 shows that after the ReDim statement executes, the size of values1 is changed to 7 and the value of each element is reinitialized to the default value of the type of the array element (that is, 0 for Integers). To save the original data stored in an array, follow the ReDim keyword with the optional Preserve © by Pearson Education, Inc. All Rights Reserved.


Download ppt "Visual Basic 2010 How to Program"

Similar presentations


Ads by Google