48
T U T O R I A L 2009 Pearson Education, Inc. All rights rese 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

  • Upload
    fred

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

6. Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic. Outline. 6.1 Test-Driving the Enhanced Inventory Application 6.2 Variables 6.3 Handling the TextChanged Event 6.4 Memory Concepts 6.5 Arithmetic 6.6 Using the Debugger: Breakpoints. - PowerPoint PPT Presentation

Citation preview

Page 1: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

T U T O R I A L

2009 Pearson Education, Inc. All rights reserved.

1

6Enhancing the

InventoryApplication

Introducing Variables, Memory Concepts and Arithmetic

Page 2: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

2

Outline

6.1 Test-Driving the Enhanced Inventory Application

6.2 Variables6.3 Handling the TextChanged Event6.4 Memory Concepts6.5 Arithmetic6.6 Using the Debugger: Breakpoints

Page 3: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

3

In this tutorial you will learn: ■ Create variables.■ Handle the TextChanged event.■ Apply basic memory concepts using variables.■ Understand the precedence rules of arithmetic

operators.■ Set breakpoints to debug applications.

Objectives

Page 4: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

Application Requirements

2009 Pearson Education, Inc. All rights reserved.

4

6.1 Test-Driving the Enhanced Inventory Application

The inventory manager notices a flaw in your Inventory application. Although the application calculates the correct result, that result continues to display even after new data is entered. The only time the output changes is when the inventory manager clicks the Calculate Total Button again. You need to alter the Inventory application to clear the result as soon as the user enters new information into either of the TextBoxes, to avoid any confusion over the accuracy of your calculated result.

Page 5: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

5

■ Open Inventory3.sln to test-drive the application (Fig. 6.1).

Figure 6.1 | Inventory application GUI displayed when the application runs.

Test-Driving the Enhanced Inventory Application

Page 6: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

6

■ Enter a value into each TextBox and click Calculate Total (Fig. 6.2).

Figure 6.2 | Running the Inventory application.

Test-Driving the EnhancedInventory Application (Cont.)

Page 7: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

7

■ The result displayed in the Total: Label will be removed (Fig. 6.3) when the user enters a new quantity in either TextBox.

Figure 6.3 | Enhanced Inventory application clears output Label after new input.

Test-Driving the EnhancedInventory Application (Cont.)

Cleared output Label

Page 8: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

8

■ A variable holds data for your application.– Unlike the Text property of a Label, variable values are

not shown to the user by default.– Using variables in an application allows you to store and

manipulate data.– Variables store data such as numbers, the date, the time

and so on.– However, each variable used in Visual Basic corresponds

to exactly one type of information.■ All variables must be declared by using program

code.– Declarations that you’ll make within event handlers begin

with the keyword Dim.

6.2 Variables

Page 9: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

9

■ Open the Inventory application’s template file. ■ Enter Code view by selecting View > Code (Fig. 6.4).■ Lines 8–10 declare that variables cartons, items and result store data of type Integer, using the As keyword.

Figure 6.4 | Declaring variables in event handler calculateButton_Click.

Using Variables in the Inventory Application

Variable declarations

Click event handler

Page 10: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

10

Good Programming Practice

Naming your variables:

Use only letters and digits as characters for your variable names.

Page 11: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

11

Good Programming Practice

Typically:

•Variable-name identifiers begin with a lowercase letter

• Every word in the name after the first word should begin with a capital letter for example,

firstNumberThis is often called camel case.

Page 12: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

12

■ Once the user enters numbers and clicks Calculate Total, the values found in the Text property of each TextBox control are converted to numerical values by the Val function.

■ Line 13 (Fig. 6.5) is read as “cartons gets the result of the Val function applied to cartonsTextBox.Text.”

Figure 6.5 | Retrieving numerical input from TextBoxes.

Using Variables in theInventory Application (Cont.)

Assigning user input to variables

Page 13: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

13

■ The Val function returns a numerical value as data type Double when converting a value retrieved from a TextBox’s Text property.

– Data type Double is used to store both whole and fractional numbers.

– Normally, Doubles store floating-point numbers, which are numbers with decimal points such as 2.3456and –845.4680.

6.2 Variables (Cont.)

Page 14: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

14

■ Lines 13–14 implicitly convert the Doubles to Integer values. This process is called implicit conversion because the conversion is performed by Visual Basic without any additional code.

– Implicit conversions from Double to Integer are generally considered poor programming practice due to the potential loss of information.

6.2 Variables (Cont.)

Page 15: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

15

■ Visual Basic defines 15 primitive data types (listed in Fig. 6.6), such as Integer.

■ Primitive data type names are also keywords.■ Visual Basic also defines the type Object. ■ Together, the primitive data types and type Object are known as built-in data types.

6.2 Variables (Cont.)

Figure 6.6 | Visual Basic built-in data types.

Built in (primitive) data types

Boolean Date Integer Long Short

Byte Decimal Single Char Double

SByte String UInteger ULong UShort

Page 16: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

16

■ The statement in line 17 (Fig. 6.7) multiplies the Integer variable cartons by items and assigns the result to variable result, using the assignment operator =.

■ The statement is read as, “result gets the value of cartons * items.”

Figure 6.7 | Multiplication, using variables in calculateButton_Click.

Using Variables in a Calculation

Calculating anddisplaying the result

Page 17: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

17

■ Line 20 assigns the calculation’s result to totalResultLabel’s Text property. The Label then displays the result (Fig. 6.8).

Figure 6.8 | Displaying the multiplication result using variables.

Using Variables in a Calculation (Cont.)

Result ofcalculation

Page 18: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

18

■ Double click the Cartons per shipment: TextBox to generate an event handler for the TextChanged event (Fig. 6.9).

■ The notation "" in line 28 is called an empty string, which is a value that does not contain any characters.

Handling the TextChanged Event

Figure 6.9 | TextChanged event handler for Cartons per shipment: TextBox.

TextChanged event handler

Page 19: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

19

■ You want the result cleared regardless of which TextBox changes value first.

■ Double click the Items per carton: TextBox, and add these lines (Fig. 6.10) to perform the same task as Line 28.

Figure 6.10 | TextChanged event handler for Items per carton: TextBox.

Handling the TextChanged Event (Cont.)

Page 20: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

20

1 Public Class InventoryForm 2 ' handles Click event 3 Private Sub calculateButton_Click(ByVal sender As _ 4 System.Object, ByVal e As System.EventArgs) _ 5 Handles calculateButtton.Click 6 7 ' declare variables 8 Dim cartons As Integer 9 Dim items As Integer 10 Dim result As Integer 11 12 ' retrieve numbers from TextBoxes 13 cartons = Val(cartonsTextBox.Text) 14 items = Val(itemsTextBox.Text) 15 16 ' multiply two numbers 17 result = cartons * items 18

■ Figure 6.11 presents the source code for theInventory application.

Use keyword Dim to declare variables insidean event handler

Outline

(1 of 2 )

Assigning a property’s value to a variable

Page 21: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

21

19 ' display result in Label 20 totalResultLabel.Text = result 21 End Sub ' calculateButton_Click 22 23 ' handles TextChanged event for cartonsTextBox 24 Private Sub cartonsTextBox_TextChanged(ByVal sender As _ 25 System.Object, ByVal e As System.EventArgs) _ 26 Handles cartonsTextBox.TextChanged 27 28 totalResultLabel.Text = "" ' clear output Label 29 End Sub ' cartonsTextBox_TextChanged 30 31 ' handles TextChanged event for itemsTextBox 32 Private Sub itemsTextBox_TextChanged(ByVal sender As _ 33 System.Object, ByVal e As System.EventArgs) _ 34 Handles itemsTextBox.TextChanged 35 36 totalResultLabel.Text = "" ' clear output Label 37 End Sub ' itemsTextBox_TextChanged 38 End Class ' InventoryForm

Outline

(2 of 2 )

Assigning avariable’s valueto a property

Setting a Label’s Text property to an empty string

Page 22: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

22

Good Programming Practice

If a statement is wider than the code editor window, use the line-continuation character to continue it on the next line.

Page 23: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

23

■ Variable names—such as cartons, items and result—correspond to actual locations in the computer’s memory.

■ Every variable has a name, type, size and value. cartons = Val(cartonsTextBox.Text)

■ Suppose that the user enters the characters 12 in the Cartons per shipment: TextBox. When the user clicks Calculate Total, the user input is converted to a Double using Val, then the Double value is implicitly converted to an Integer.

6.4 Memory Concepts

Page 24: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

24

■ The assignment then places the Integer value 12 in the location for variable cartons, as shown in Figure 6.12.

Figure 6.12 | Memory location showing name and value of variable cartons.

6.4 Memory Concepts (Cont.)

Page 25: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

25

■ Whenever a value is placed in a memory location,this value replaces the value previously stored in that location.

■ The previous value is overwritten (lost).

6.4 Memory Concepts (Cont.)

Page 26: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

26

Figure 6.13 | Memory locations after values for variablescartons and items have been input.

6.4 Memory Concepts (Cont.)

■ Suppose that the user then enters the characters 10 in the Items per carton: TextBox and clicks Calculate Total (Fig. 6.13). items = Val(itemsTextBox.Text)

Page 27: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

27

■ Once the Calculate Total Button is clicked, line 17 multiplies these values and places their total into variable result.

result = cartons * items■ This performs the multiplication and replaces result’s previous value.

6.4 Memory Concepts (Cont.)

Page 28: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

28

Figure 6.14 | Memory locations after a multiplication operation.

6.4 Memory Concepts (Cont.)

■ The values of cartons and items appear exactly as they did before they were used in the calculation of result (Fig. 6.14).

■ This illustrates that when a value is read from a memory location, the process is non destructive.

Page 29: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

29

■ The arithmetic operators are summarized in Figure 6.15.■ Note the use of various special symbols not used in

algebra.

6.5 Arithmetic

Figure 6.15 | Arithmetic operators.

Visual Basic .NET operation

Arithmetic operator

Algebraic expression

Visual Basic 2008 expression

Addition + f + 7 f + 7

Subtraction – p – c p - c

Multiplication * bm b * m

Division (float) / or orxx y x ÷ yy

x / y

Division (integer) \ none v \ u

Modulus Mod r mod s r Mod s

Exponentiation ^ qp q ^ p

Unary Negative - –e –e

Unary Positive + +g +g

Page 30: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

30

■ Visual Basic has separate operators for integer division (the backslash, \) and floating-point division (the forward slash, /).

– Floating-point division divides two numbers and returns a floating-point number.

– The operator for integer division treats its operands as integers and returns an integer result.

6.5 Arithmetic (Cont.)

Page 31: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

31

■ When floating-point numbers (numbers with decimal points) are used with the integer-division operator, the numbers are first rounded as follows:

– numbers ending in .5 are rounded to the nearest even integer—for example, 6.5 rounds down to 6 and 7.5 rounds up to 8

– all other floating-point numbers are rounded to the nearest integer—for example, 7.1 rounds down to 7, and 7.7 roundsup to 8.

6.5 Arithmetic (Cont.)

Page 32: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

32

Common Programming Error

Attempting to divide by zero is a runtime error (that is, an error that has its effect while the application executes). Dividing by zero terminates an application.

Page 33: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

33

■ The modulus operator, Mod, yields the remainderafter division.

– Thus, 7 Mod 4 yields 3, and 17 Mod 5 yields 2.– This operator is used most commonly with Integer

operands.– The modulus operator can be used to discover whether

one number is a multiple of another.

6.5 Arithmetic (Cont.)

Page 34: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

34

■ Arithmetic expressions in Visual Basic must be written in straight-line form so that you can type them into a computer.

– For example, the division of 7.1 by 4.3 must bewritten as 7.1 / 4.3.

– Also, raising 3 to the second power cannot be writtenas 32 but is written in straight-line form as 3 ^ 2.

■ Parentheses are used in Visual Basic expressions to group operations in the same manner as in algebraic expressions. To multiply a times the quantity b + c, you writea * ( b + c )

6.5 Arithmetic (Cont.)

Page 35: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

35

■ Visual Basic applies the operators in arithmetic expressions in a precise sequence, determined by the rules of operator precedence.

■ Operators of the same type are applied from left to right.

6.5 Arithmetic (Cont.)

Page 36: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

36

■ Operators in expressions contained within a pair of parentheses are evaluated first.

– With nested (or embedded) parentheses, the operators contained in the innermost pair of parentheses areapplied first.

■ Exponentiation is applied next.■ Unary positive and negative, + and -, are

applied next.

Rules of Operator Precedence

Page 37: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

37

■ Multiplication and floating-point division operations are applied next.

■ Integer division is applied next. ■ Modulus operations are applied next.■ Addition and subtraction operations are

applied last.

Rules of Operator Precedence (Cont.)

Page 38: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

38

■ Let’s consider several expressions in light of therules of operator precedence.

– The following calculates the average of three numbers:Algebra: m = ( a + b + c )

3Visual Basic: m = ( a + b + c ) / 3– The parentheses are required because floating-point division has

higher precedence than addition.– The following is the equation of a straight line:Algebra: y = mx + b Visual Basic: y = m * x + b– No parentheses are required due to operator precedence.

6.5 Arithmetic (Cont.)

Page 39: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

39

■ To develop a better understanding of the rules of operator precedence, consider how the expressiony = ax2 + bx + c is evaluated:

■ The circled numbers under the statement indicatethe order in which Visual Basic applies the operators.

■ It is acceptable to place redundant parentheses in an expression to make the expression easier to read.y = ( a * ( x ^ 2 ) ) + ( b * x ) + c

6.5 Arithmetic (Cont.)

Page 40: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

40

Good Programming Practice

Using redundant parentheses in complex arithmetic expressions can make the expressions easier to read.

Page 41: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

41

■ A breakpoint is a marker that can be set at any executable line of code.

■ When application execution reaches a breakpoint, execution pauses, allowing you to peek inside your application and ensure that there are no logic errors.

6.6 Using the Debugger: Breakpoints

Page 42: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

42

■ To insert a breakpoint in the IDE, either click inside the margin indicator bar next to a line of code, or right click that line of code and select Breakpoint > Insert Breakpoint Fig. 6.16).

■ The application is said to be in break mode when the debugger pauses the application’s execution.

Using the Debugger: Breakpoints

Figure 6.16 | Setting two breakpoints.

Margin indicator bar

Breakpoints

Page 43: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

43

■ After setting breakpoints in the code editor, select Debug > Start Debugging to begin the debugging process (Fig. 6.17, Fig. 6.18).

Using the Debugger: Breakpoints (Cont.)

Figure 6.17 | Inventory application running.

Figure 6.18 | Title bar of the IDE displaying (Debugging).

Title bar displays (Debugging)

Page 44: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

44

■ Application execution suspends at the first breakpoint, and the IDE becomes the active window (Fig. 6.19). The yellow arrow to the left of line 17 indicates that this line contains the next statement to execute.

Using the Debugger: Breakpoints (Cont.)

Figure 6.19 | Application execution suspended at the first breakpoint.

Yellow arrowBreakpoints

Next executable statement

Page 45: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

45

■ To resume execution, select Debug > Continue (or press F5).

■ Note that when you place your mouse pointer over the variable name result, the value that the variable stores is displayed in a Quick Info box (Fig. 6.20).

Using the Debugger: Breakpoints (Cont.)

Figure 6.20 | Displaying a variable value by placing the mouse pointer over a variable name.

Quick Info boxdisplays variable result’s value

Page 46: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

46

■ Use the Debug > Continue command to complete the application execution. When there are no more breakpoints at which to suspend execution, the application executes to completion (Fig. 6.21).

Using the Debugger: Breakpoints (Cont.)

Figure 6.21 | Application output.

Page 47: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

47

■ To disable a breakpoint, right click a line of code in which a breakpoint has been set, and select Breakpoint > Disable Breakpoint (Fig. 6.22).

Using the Debugger: Breakpoints (Cont.)

Figure 6.22 | Disabled breakpoint.

Disabled breakpoint

Page 48: Enhancing the  Inventory Application Introducing Variables, Memory Concepts and Arithmetic

2009 Pearson Education, Inc. All rights reserved.

48

■ To fully remove a breakpoint, right click a line of code in which a breakpoint has been set and select Breakpoint > Delete Breakpoint.

■ To remove all breakpoints, select Debug > Delete All Breakpoints from the menu bar.

Using the Debugger: Breakpoints (Cont.)