Visual Basic 2010 How to Program

Slides:



Advertisements
Similar presentations
Visual Basic 2010 How to Program
Advertisements

Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Arrays 1.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
Arrays Chapter 7.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 11 - JavaScript: Arrays
Microsoft Visual Basic 2005: Reloaded Second Edition
© 2016 Pearson Education, Ltd. All rights reserved.
Control Structures: Part 2
Chapter 7 Arrays.
JavaScript: Functions.
Repeating Program Instructions
Variables and Arithmetic Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
7 Arrays.
CSCI 3327 Visual Basic Chapter 7: Arrays
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Chapter 3: Introduction to Problem Solving and Control Statements
Visual Basic 2010 How to Program
Object Oriented Programming in java
CIS 16 Application Development Programming with Visual Basic
Chapter 6 Control Statements: Part 2
Introduction to Problem Solving and Control Statements
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Computer Programming IT-104
Presentation transcript:

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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} © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

Sorting an Array © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

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}} © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

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

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

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

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.