0% found this document useful (0 votes)
8 views

Lect03 Introducing Variables, Memory Concepts, and Arithmetic

This document provides an overview of enhancing an inventory application to use variables, handle events, and perform arithmetic calculations. It introduces variables to store application data like numbers, explains how to declare and assign variables, and discusses data types in Visual Basic. The document also demonstrates handling the TextChanged event to clear output when user input changes, and using variables to perform calculations like multiplication and display the results. The overall aim is to make the inventory application more dynamic by incorporating variables, events, and arithmetic.

Uploaded by

Nourhan Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lect03 Introducing Variables, Memory Concepts, and Arithmetic

This document provides an overview of enhancing an inventory application to use variables, handle events, and perform arithmetic calculations. It introduces variables to store application data like numbers, explains how to declare and assign variables, and discusses data types in Visual Basic. The document also demonstrates handling the TextChanged event to clear output when user input changes, and using variables to perform calculations like multiplication and display the results. The overall aim is to make the inventory application more dynamic by incorporating variables, events, and arithmetic.

Uploaded by

Nourhan Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

1

T U T O R I A L

Enhancing the
6
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 Variables
6.3 Handling the TextChanged Event
6.4 Memory Concepts
6.5 Arithmetic
6.6 Using the Debugger: Breakpoints

 2009 Pearson Education, Inc. All rights reserved.


3

Objectives

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.

 2009 Pearson Education, Inc. All rights reserved.


4

6.1 Test-Driving the Enhanced Inventory Application


Application Requirements

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.

 2009 Pearson Education, Inc. All rights reserved.


5

Test-Driving the Enhanced Inventory Application

■ Open Inventory3.sln to test-drive the


application (Fig. 6.1).

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

 2009 Pearson Education, Inc. All rights reserved.


6
Test-Driving the Enhanced
Inventory Application (Cont.)

■ Enter a value into each TextBox and click


Calculate Total (Fig. 6.2).

Figure 6.2 | Running the Inventory application.

 2009 Pearson Education, Inc. All rights reserved.


7
Test-Driving the Enhanced
Inventory Application (Cont.)

■ The result displayed in the Total: Label will be


removed (Fig. 6.3) when the user enters a new
quantity in either TextBox.

Cleared output Label

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

 2009 Pearson Education, Inc. All rights reserved.


8

6.2 Variables

■ 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.
 2009 Pearson Education, Inc. All rights reserved.
9

Using Variables in the Inventory Application

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

Click event handler

Variable declarations

Figure 6.4 | Declaring variables in event handler calculateButton_Click.

 2009 Pearson Education, Inc. All rights reserved.


10

Good Programming Practice


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

 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,
firstNumber. This is often called camel case.

 2009 Pearson Education, Inc. All rights reserved.


12
Using Variables in the
Inventory Application (Cont.)

■ 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.”

Assigning user input to variables

Figure 6.5 | Retrieving numerical input from TextBoxes.

 2009 Pearson Education, Inc. All rights reserved.


13

6.2 Variables (Cont.)

■ 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.3456
and –845.4680.

 2009 Pearson Education, Inc. All rights reserved.


14

6.2 Variables (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


15

6.2 Variables (Cont.)

■ 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.
Built in (primitive) data types
Boolean Date Integer Long Short
Byte Decimal Single Char Double
SByte String UInteger ULong UShort

Figure 6.6 | Visual Basic built-in data types.

 2009 Pearson Education, Inc. All rights reserved.


16

Using Variables in a Calculation

■ 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.”

Calculating and
displaying the result

Figure 6.7 | Multiplication, using variables in calculateButton_Click.

 2009 Pearson Education, Inc. All rights reserved.


17

Using Variables in a Calculation (Cont.)

■ Line 20 assigns the calculation’s result to


totalResultLabel’s Text property. The Label
then displays the result (Fig. 6.8).

Result of
calculation

Figure 6.8 | Displaying the multiplication result using variables.

 2009 Pearson Education, Inc. All rights reserved.


18

Handling the TextChanged Event

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

TextChanged event handler

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

 2009 Pearson Education, Inc. All rights reserved.


19

Handling the TextChanged Event (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


20
Outline

■ Figure 6.11 presents the source code for the


Inventory application. (1 of 2 )

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 Use keyword Dim to declare
8 Dim cartons As Integer variables inside
9 Dim items As Integer an event handler
10 Dim result As Integer
11
12 ' retrieve numbers from TextBoxes
Assigning a property’s value to a
13 cartons = Val(cartonsTextBox.Text)
variable
14 items = Val(itemsTextBox.Text)
15
16 ' multiply two numbers
17 result = cartons * items
18

 2009 Pearson Education,


Inc. All rights reserved.
21
Outline

(2 of 2 )
19 ' display result in Label Assigning a
20 totalResultLabel.Text = result variable’s value
21 End Sub ' calculateButton_Click to a property
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
Setting a Label’s Text
28 totalResultLabel.Text = "" ' clear output Label
property to an empty string
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

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

 2009 Pearson Education, Inc. All rights reserved.


23

6.4 Memory Concepts

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

 2009 Pearson Education, Inc. All rights reserved.


24

6.4 Memory Concepts (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


25

6.4 Memory Concepts (Cont.)

■ 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).

 2009 Pearson Education, Inc. All rights reserved.


26

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)

Figure 6.13 | Memory locations after values for variables


cartons and items have been input.

 2009 Pearson Education, Inc. All rights reserved.


27

6.4 Memory Concepts (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


28

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.

Figure 6.14 | Memory locations after a multiplication operation.

 2009 Pearson Education, Inc. All rights reserved.


29

6.5 Arithmetic

■ The arithmetic operators are summarized in Figure 6.15.


■ Note the use of various special symbols not used in
algebra.
Visual Basic .NET Arithmetic Algebraic expression Visual Basic 2008
operation operator expression
Addition + f+7 f + 7

Subtraction – p–c p - c

Multiplication * bm b * m
Division (float) / x x / y
x y or or x ÷ y
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

Figure 6.15 | Arithmetic operators.


 2009 Pearson Education, Inc. All rights reserved.
30

6.5 Arithmetic (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


31

6.5 Arithmetic (Cont.)

■ 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 rounds
up to 8.

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

 2009 Pearson Education, Inc. All rights reserved.


33

6.5 Arithmetic (Cont.)

■ The modulus operator, Mod, yields the remainder


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

 2009 Pearson Education, Inc. All rights reserved.


34

6.5 Arithmetic (Cont.)

■ 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 be
written as 7.1 / 4.3.
– Also, raising 3 to the second power cannot be written
as 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
write
a * ( b + c )

 2009 Pearson Education, Inc. All rights reserved.


35

6.5 Arithmetic (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


36

Rules of Operator Precedence

■ 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 are
applied first.
■ Exponentiation is applied next.
■ Unary positive and negative, + and -, are
applied next.

 2009 Pearson Education, Inc. All rights reserved.


37

Rules of Operator Precedence (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.


38

6.5 Arithmetic (Cont.)

■ Let’s consider several expressions in light of the


rules of operator precedence.
– The following calculates the average of three numbers:
Algebra: m=(a+b+c)
3
Visual 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.

 2009 Pearson Education, Inc. All rights reserved.


39

6.5 Arithmetic (Cont.)

■ To develop a better understanding of the rules of


operator precedence, consider how the expression
y = ax2 + bx + c is evaluated:

■ The circled numbers under the statement indicate


the 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

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

 2009 Pearson Education, Inc. All rights reserved.


41

6.6 Using the Debugger: Breakpoints

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

 2009 Pearson Education, Inc. All rights reserved.


42

Using the Debugger: Breakpoints

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

Margin indicator bar

Breakpoints

Figure 6.16 | Setting two breakpoints.


 2009 Pearson Education, Inc. All rights reserved.
43

Using the Debugger: Breakpoints (Cont.)

■ After setting breakpoints in the code editor, select


Debug > Start Debugging to begin the
debugging process (Fig. 6.17, Fig. 6.18).

Figure 6.17 | Inventory application running.


Title bar displays (Debugging)

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

 2009 Pearson Education, Inc. All rights reserved.


44

Using the Debugger: Breakpoints (Cont.)

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

Yellow arrow Next executable statement

Breakpoints

Figure 6.19 | Application execution suspended at the first breakpoint.

 2009 Pearson Education, Inc. All rights reserved.


45

Using the Debugger: Breakpoints (Cont.)

■ 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).

Quick Info box


displays variable
result’s value

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

 2009 Pearson Education, Inc. All rights reserved.


46

Using the Debugger: Breakpoints (Cont.)

■ 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).

Figure 6.21 | Application output.

 2009 Pearson Education, Inc. All rights reserved.


47

Using the Debugger: Breakpoints (Cont.)

■ 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).

Disabled breakpoint

Figure 6.22 | Disabled breakpoint.

 2009 Pearson Education, Inc. All rights reserved.


48

Using the Debugger: Breakpoints (Cont.)

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

 2009 Pearson Education, Inc. All rights reserved.

You might also like