Title: Mgmt 362a Enhancing the Inventory Application
16
- Mgmt 362a Enhancing the Inventory Application
- Introducing Variables, Memory Concepts and
Arithmetic
2Outline
- 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
- 6.7 Internet and Web Resources
- 6.8 Wrap-Up
3Objectives
- In this tutorial you will learn
- Create variables.
- Handle the TextChanged event.
- Apply basic memory concepts using variables.
- Use the precedence rules of arithmetic operators.
- Set breakpoints to debug applications.
46.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 timethe output changes is when
the inventory manager clicks theCalculate Button
again. You need to alter the Inventory
applicationto clear the result as soon as the
user enters new information intoeither of the
TextBoxes, to avoid any confusion over the
accuracy of your calculated result.
56.1 Test-Driving the Enhanced Inventory
Application (Cont.)
- Figure 6.1 Inventory application GUI displayed
when the application runs.
Running the application
66.1 Test-Driving the Enhanced Inventory
Application (Cont.)
- Figure 6.2 Running the Inventory application.
- Using the application
- Enter data into the TextBoxes and click the
Calculate Total Button - Notice the result
76.1 Test-Driving the Enhanced Inventory
Application (Cont.)
- Figure 6.3 Enhanced Inventory application
clears output Label after new input.
- Notice the new feature
- The output Label is cleared when new data is
entered
86.2 Variables
- Adding code to the application
- Declaring a variable
- Keyword Dim
- Data types
- Built-in data type
- Primitive data type
9Good Programming Practice
- Typically, variable-name identifiers begin witha
lowercase letter. Every word in the name
afterthe first word should begin with a capital
letter,for example, firstNumber.
10Good Programming Practice
- Use only letters and digits as characters
foryour variable names.
116.2 Using Variables in the Inventory Application
- Figure 6.4 Declaring variables in event handler
calculateButton_Click.
126.2 Using Variables in the Inventory Application
(Cont.)
- Retrieving data from the user
- Data is stored in the TextBoxs Text property
- The Val function
- Returns data type Double
- Implicit conversion
136.2 Using Variables in the Inventory Application
(Cont.)
- Figure 6.5 Retrieving numerical input from
TextBoxes.
146.2 Using Variables in the Inventory Application
(Cont.)
- Figure 6.6 Visual Basic built-in data types.
156.2 Using Variables in the Inventory Application
(Cont.)
- Perform the multiplication
- Displaying the result
- Set the multiplication result to the Labels Text
property
166.2 Using Variables in the Inventory Application
(Cont.)
- Figure 6.7 Multiplication, using variables in
calculateButton_Click.
176.2 Using Variables in the Inventory Application
(Cont.)
- Figure 6.8 Displaying the multiplication result
using variables.
- Running the application
- Enter data
- Click the Calculate Total Button
- Notice the result
186.3 Handling the TextChanged Event
- Create the event handler for the first TextBox
- Double click the TextBox
- TextChanged event handler is created
- Clearing the value
- Use empty string
- Repeat for second TextBox
196.3 Handling the TextChanged Event (Cont.)
- Figure 6.9 TextChanged event handler for
Cartons per shipment TextBox.
20Good Programming Practice
- If a statement is wider than the code editor
window, use the line-continuation character
within the statement to continue it on the next
line.
216.3 Handling the TextChanged Event (Cont.)
- Figure 6.10 TextChanged event handler for Items
per carton TextBox.
22Outline
(1 of 2 )
Use keyword Dim to declare variables inside an
event handler
Assigning a propertys valueto a variable
Assigning a variableto a property
23Outline
(2 of 2 )
Setting a TextBoxs Text property to an empty
string
246.4 Memory Concepts
- Figure 6.12 Memory location showing name and
value of variable cartons.
- A simple variable
- Corresponds to actual location in the computers
memory - Name
- Type
- Size
- Value
256.4 Memory Concepts (Cont.)
- Figure 6.13 Memory locations after values for
variables cartons anditems have been input.
- Visualizing data
- The value of each variable is stored in a
separate memory location
266.4 Memory Concepts (Cont.)
- Figure 6.14 Memory locations after a
multiplication operation.
- Using the variables data
- Nondestructive when value is read from memory
276.5 Arithmetic
- Operator precedence
- Operators in expressions contained within a pair
of parentheses are evaluated first - Exponentiation is applied next
- Unary positive and negative are applied
- Multiplication and floating-point division
operations - Division is applied
- Modulus operations are second to last
- Lastly, Addition and subtraction operations
286.5 Arithmetic (Cont.)
Figure 6.15 Arithmetic operators.
29Common 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
zeroterminates an application.
30Good Programming Practice
- Using redundant parentheses in complex arithmetic
expressions can make the expressions easier to
read.
316.6 Using the Debugger Breakpoints
- Methods of inserting breakpoints
- Click inside the margin indicator bar
- Right click the line of code and select
Breakpoint gt Insert Breakpoint - Breakpoints
- You can set as many as you like
- Solid maroon circle appears for each breakpoint
- Can be set during design more, break mode, or run
mode
326.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.16 Setting two breakpoints.
336.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.17 Inventory application running.
Run the application Click the Calculate Total
Button
346.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.18 Title bar of the IDE displaying
(Debugging).
The IDE title bar displays the applications mode
356.6 Using the Debugger Breakpoints (Cont.)
- Debugging the application
- Program execution suspends at the first break
point - Yellow arrow indicate next statement to execute
366.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.19 Application execution suspended at
the first breakpoint.
376.6 Using the Debugger Breakpoints (Cont.)
- To resume execution, select Debug gt Continue
- The application continues its normal execution
- Quick Info box
- Displays variables current value
386.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.20 Displaying a variable value by
placing the mouse pointer overa variable name.
396.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.21 Application output.
- Use the Debug gt Continue command to complete the
application - No more breakpoints, program will not suspend
- The entire application executes to completion
406.6 Using the Debugger Breakpoints (Cont.)
- Disabling a breakpoint
- Right click the line of code with the breakpoint
- Select Breakpoint gt Disable Breakpoint
- A hollow maroon circle indicates disabled
breakpoint
416.6 Using the Debugger Breakpoints (Cont.)
- Figure 6.22 Disabled breakpoint.
426.6 Using the Debugger Breakpoints (Cont.)
- Removing a breakpoint
- Right click the line of code with the breakpoint
- Select Breakpoint gt Remove Breakpoint
- Or click the maroon circle in the margin
indicator bar