VB 03
VB 03
VB 03
Reloaded
Chapter Three
Memory Locations and Calculations
2
Overview
■ Class Definitions
■ Events and Event Handlers
■ Adding Code to an Event Handler
■ Val Function and TextChanged Event
■ Declaring Variables
■ TryParse and Convert Class Methods
■ Arithmetic (+,-,*,/,\,^,Mod)
■ Variable Scope and Lifetime
■ Option Explicit, Infer and Strict
■ Debugging Errors and Debugger Breakpoints
2009 Pearson Education, Inc. All rights reserved.
3
Class Definitions
Class Definitions
End Sub
■ Button: Click
■ Text Box: TextChanged
■ Radio Button: CheckedChanged
■ Check Box: CheckedChanged
■ Combo Box: SelectedIndexChanged
■ Enter
■ TextChanged
■ KeyDown
■ MouseEnter
■ MouseLeave
■ MouseHover
totalResultLabel.Text = cartonsTextBox.Text _
* itemsTextBox.Text
End Sub
End Class
totalResultLabel.Text = Val(cartonsTextBox.Text) * _
Val(itemsTextBox.Text)
End Sub
End Class
Val Function
Val Function
Val Function
Figure 6.3 | Enhanced Inventory application clears output Label after new input.
TextChanged Event
Figure 6.9 | TextChanged event handler for Cartons per shipment: TextBox.
Variables
Implicit Conversion
■ TryParse method:
Part of every numeric data type’s class
Used to convert a string to that numeric data type
■ Argument: a value that is provided to a method
■ Basic syntax of TryParse method has two
arguments:
String: string value to be converted
Variable: location to store the result
■ If TryParse conversion is successful, the
method stores the value in the variable
■ If unsuccessful, a 0 is stored in the numeric
variable 2009 Pearson Education, Inc. All rights reserved.
Figure 3-6: How to use the basic syntax of the TryParse method
■ Convert class:
Contains methods for converting numeric values to specific
data types
■ Commonly used methods of the Convert class
include:
ToDouble
ToDecimal
ToInt32
ToString
Arithmetic
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
Exponentiation ^ qp q ^ p
Unary Negative - –e –e
Unary Positive + +g +g
Arithmetic
Modulus Operator
■ Static variable:
Procedure-level variable that remains in memory and
retains its value even after the procedure ends
Retains its value until the application ends (like a class-level
variable), but can only be used by the procedure in which
it is declared
■ A static variable has:
Same lifetime as a class-level variable
Narrower scope than a class-level variable
■ Declared using the Static keyword
Debugging Errors
Debugging Errors
Debugger Breakpoints
Debugger Breakpoints
Breakpoints
Debugger Breakpoints
Debugger Breakpoints
Debugger Breakpoints
Figure 6.20 | Displaying a variable value by placing the mouse pointer over a variable name.
Debugger Breakpoints