620 likes | 888 Views
Microsoft Visual Basic 2008: Reloaded, Third Edition. 2. Objectives. After studying this chapter, you should be able to:Declare variables and named constantsAssign data to an existing variableConvert data to the appropriate type using the TryParse method and the Convert class methodsWrite arithm
E N D
1. Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Three
Variables, Constants,
and Arithmetic Operators
2. Microsoft Visual Basic 2008: Reloaded, Third Edition 2 Objectives After studying this chapter, you should be able to:
Declare variables and named constants
Assign data to an existing variable
Convert data to the appropriate type using the TryParse method and the Convert class methods
Write arithmetic expressions
Understand the scope and lifetime of variables and named constants
3. Microsoft Visual Basic 2008: Reloaded, Third Edition 3 Objectives (continued) Understand the purpose of the Option statements
Use a TOE chart, pseudocode, and a flowchart to code an application
Clear the contents of a control’s Text property while an application is running
Send the focus to a control while the application is running
Explain the difference between syntax errors and logic errors
Format an application’s numeric output
4. Microsoft Visual Basic 2008: Reloaded, Third Edition 4 Variables Variables: computer memory locations used to store data while an application is running
Use a meaningful variable name that reflects the purpose of the variable
Use camel casing for variable identifiers
Variable names should conform to naming rules
5. Variables (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 5
6. Variables (continued) Each variable must be assigned a data type
Data type: the type of data the variable can store
Each data type is a class
Unicode:
Universal coding scheme for characters
Assigns a unique numeric value to each character
Microsoft Visual Basic 2008: Reloaded, Third Edition 6
7. Microsoft Visual Basic 2008: Reloaded, Third Edition 7
8. Microsoft Visual Basic 2008: Reloaded, Third Edition 8 Declaring a Variable in Code Declaration statement: used to declare, or create, a variable
Declaration statement includes
Scope keyword: Dim, Private, or Static
Name of the variable
Data type
Initial value (optional)
Numeric data types are automatically initialized to 0
String data type is automatically initialized to “”
9. Microsoft Visual Basic 2008: Reloaded, Third Edition 9
10. Microsoft Visual Basic 2008: Reloaded, Third Edition 10 Assigning Data to an Existing Variable Assignment statement:
Used to assign values to properties of controls
Used to assign values to variables
Assignment operator: (=)
Value on the right of the = operator is assigned to the variable on the left of the = operator
11. Assigning Data to an Existing Variable (continued) String: group of characters enclosed in quotation marks
Literal constant:
An item of data whose value does not change while the application is running
Can be a numeric or a string literal constant
A numeric literal without a decimal place is treated as an integer
A numeric literal with a decimal place is treated as a Double type Microsoft Visual Basic 2008: Reloaded, Third Edition 11
12. Microsoft Visual Basic 2008: Reloaded, Third Edition 12
13. Using the TryParse Method Method: a specific portion of a class’s instructions that performs a task for the class
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 13
14. Using the TryParse Method (continued) If TryParse conversion is successful, the method stores the value in the variable
If unsuccessful, a 0 is stored in the numeric variable
Microsoft Visual Basic 2008: Reloaded, Third Edition 14
15. Microsoft Visual Basic 2008: Reloaded, Third Edition 15
16. Using the Convert Class Convert class:
Contains methods for converting numeric values to specific data types
Use the dot member access operator to separate the class name from the method name
Commonly used methods of the Convert class include:
ToDouble
ToDecimal
ToInt32
ToString
Microsoft Visual Basic 2008: Reloaded, Third Edition 16
17. Microsoft Visual Basic 2008: Reloaded, Third Edition 17
18. Option Explicit, Option Infer, and Option Strict Undeclared variable: a variable that does not appear in a declaration statement (such as Dim)
Is assigned a data type of Object
Misspelling a variable name can result in an undeclared variable unless Option Explicit is on
Option Explicit On statement
Appears in the General Declarations section of the Code Editor window (above Public Class statement)
Enforces that all variables must be declared before being used Microsoft Visual Basic 2008: Reloaded, Third Edition 18
19. Option Explicit, Option Infer, and Option Strict (continued) Option Infer Off statement: ensures that every variable is declared with a data type
Implicit type conversion: occurs when you attempt to assign data of one type to a variable of another type without explicitly attempting to convert it
If converted to a data type that can store larger numbers, the value is said to be promoted
If converted to a data type that can store only smaller numbers, the value is said to be demoted
Can cause truncation and loss of precision Microsoft Visual Basic 2008: Reloaded, Third Edition 19
20. Option Explicit, Option Infer, and Option Strict (continued) Option Strict On statement: ensures that values cannot be converted from one data type to a narrower data type, resulting in lost precision Microsoft Visual Basic 2008: Reloaded, Third Edition 20
21. Microsoft Visual Basic 2008: Reloaded, Third Edition 21
22. Microsoft Visual Basic 2008: Reloaded, Third Edition 22 Using a Variable in an Arithmetic Expression Arithmetic operators: used to perform calculations
Precedence number: indicates the order in which an operation in an expression is performed
If an expression has two operators with the same precedence, they are evaluated from left to right
Use parentheses to change the order of evaluation
Integer division operator (\): divides two integers and returns an integer value
Modulus arithmetic operator (Mod): divides two numbers and returns the remainder
23. Microsoft Visual Basic 2008: Reloaded, Third Edition 23 Using a Variable in an Arithmetic Expression (continued)
24. Microsoft Visual Basic 2008: Reloaded, Third Edition 24
25. Using a Variable in an Arithmetic Expression (continued) Line continuation character (_):
Used to break up a long instruction into two or more physical lines
Underscore must be preceded by a space and must appear at the end of a physical line
A variable can store only one value at a time
A second assignment statement on the same variable will replace its current value with the new value Microsoft Visual Basic 2008: Reloaded, Third Edition 25
26. The Scope and Lifetime of a Variable Scope: indicates where the variable can be used
Lifetime: indicates how long the variable remains in memory
Variables can have module scope, procedure scope, or block scope
Module scope: variable is declared in the form’s Declarations section
Variables declared within a procedure have either procedure scope or block scope Microsoft Visual Basic 2008: Reloaded, Third Edition 26
27. Using Variables Having Procedure Scope Procedure-level variable: declared within a procedure
Use the Dim keyword in the declaration
Procedure scope: only the procedure can use the variable
With procedure-level scope, two procedures can each use the same variable names
Comments:
Used to internally document the procedure
Are ignored by the compiler
Appear in green in the Code Editor
Microsoft Visual Basic 2008: Reloaded, Third Edition 27
28. Microsoft Visual Basic 2008: Reloaded, Third Edition 28
29. Using a Variable Having Module Scope (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 29
30. Microsoft Visual Basic 2008: Reloaded, Third Edition 30
31. Microsoft Visual Basic 2008: Reloaded, Third Edition 31
32. Microsoft Visual Basic 2008: Reloaded, Third Edition 32
33. Named Constants Named constant: memory location whose value cannot be changed while the application is running
Declared using the Const keyword
Good programming practice to specify the data type as well
Many programmers use Pascal case for named constants
Literal type character: forces a literal constant to assume a specific data type
Named constants help to document the program code
Microsoft Visual Basic 2008: Reloaded, Third Edition 33
34. Microsoft Visual Basic 2008: Reloaded, Third Edition 34
35. Microsoft Visual Basic 2008: Reloaded, Third Edition 35
36. Coding the Sunshine Cellular Application Microsoft Visual Basic 2008: Reloaded, Third Edition 36
37. Coding the Sunshine Cellular Application (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 37
38. Using Pseudocode to Plan a Procedure Pseudocode: short phrases that describe the steps a procedure needs to take to accomplish its goal
Microsoft Visual Basic 2008: Reloaded, Third Edition 38
39. Using a Flowchart to Plan a Procedure Flowchart: uses standardized symbols to show the steps a procedure must take to accomplish its goal
Can be used in place of pseudocode for planning
Three symbols:
Start/stop symbol (oval): indicates start and stop points
Process symbol (rectangle): represents tasks
Input/output symbol (parallelogram): represents input or output tasks
Flowlines: connect the symbols to show the direction Microsoft Visual Basic 2008: Reloaded, Third Edition 39
40. Microsoft Visual Basic 2008: Reloaded, Third Edition 40
41. Microsoft Visual Basic 2008: Reloaded, Third Edition 41 Coding the Calculate Order Button’s Click Event Procedure
42. Coding the Calculate Order Button’s Click Event Procedure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 42
43. Coding the Calculate Order Button’s Click Event Procedure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 43
44. Coding the Calculate Order Button’s Click Event Procedure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 44
45. Coding the Calculate Order Button’s Click Event Procedure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 45
46. Completing the Sunshine Cellular Application Microsoft Visual Basic 2008: Reloaded, Third Edition 46
47. Microsoft Visual Basic 2008: Reloaded, Third Edition 47
48. Microsoft Visual Basic 2008: Reloaded, Third Edition 48
49. Testing and Debugging the Application Bug: an error in the program code
Valid data: data that the application is expecting
Invalid data: data that is unexpected
Debugging: process of locating and correcting errors in a program Microsoft Visual Basic 2008: Reloaded, Third Edition 49
50. Testing and Debugging the Application (continued) Syntax error: an error that violates the programming language’s syntax
Usually caused by mistyping
Logic error: occurs when you enter an instruction that does not give the expected results
Test a program with both valid and invalid data Microsoft Visual Basic 2008: Reloaded, Third Edition 50
51. Microsoft Visual Basic 2008: Reloaded, Third Edition 51 Testing and Debugging the Application (continued)
52. Microsoft Visual Basic 2008: Reloaded, Third Edition 52 Testing and Debugging the Application (continued)
53. Microsoft Visual Basic 2008: Reloaded, Third Edition 53 Formatting Numeric Output Formatting: specifying the number of decimal places and any special characters to display
ToString method of a variable can be used to format a number
Format specifier: specifies the type of formatting to use
Precision specifier: controls the number of significant digits or zeros to the right of the decimal point
54. Microsoft Visual Basic 2008: Reloaded, Third Edition 54
55. Microsoft Visual Basic 2008: Reloaded, Third Edition 55 Formatting Numeric Output (continued)
56. Microsoft Visual Basic 2008: Reloaded, Third Edition 56 Formatting Numeric Output (continued)
57. Microsoft Visual Basic 2008: Reloaded, Third Edition 57 Programming Tutorial
58. Microsoft Visual Basic 2008: Reloaded, Third Edition 58 Programming Example
59. Microsoft Visual Basic 2008: Reloaded, Third Edition 59 Summary Variables and named constants are memory locations that store data
Variables can change value, but constants cannot
Variables and constants have a name, data type, initial value, scope, and lifetime
Use Dim or Static to declare a variable at block or procedure level
Use Private to declare a variable at module level
60. Summary (continued) Assignment statement is used to assign values to an existing variable
Literals are constant items of data that do not change
String literal constants are enclosed in quotation marks
Use the TryParse method to convert a string to a number
The Convert class contains methods to convert values to a specified data type Microsoft Visual Basic 2008: Reloaded, Third Edition 60
61. Microsoft Visual Basic 2008: Reloaded, Third Edition 61 Summary (continued) Option Explicit On forces declaration of all variables before use
Option Infer Off warns if a variable declaration does not include a data type
Option Strict On disallows any implicit type conversions that may cause a loss of data
Integer division operator divides two integers and returns the result
Modulus operator divides two numbers and returns the remainder
62. Summary (continued) A procedure-level variable is usable only by the procedure in which it is declared
A module-level variable is usable by all procedures in the form
Use comments to document your code
A static variable is a procedure-level variable that retains its value even when the procedure ends
Pseudocode or a flowchart is used to plan a procedure’s code
Microsoft Visual Basic 2008: Reloaded, Third Edition 62
63. Summary (continued) You can clear the contents of a text box or label control by assigning an empty string or String.Empty value
The Focus method moves the focus to a control
Test a program with both valid and invalid data
You can format a program’s numeric output with special characters, such as for currency, percentages, and number of decimal places Microsoft Visual Basic 2008: Reloaded, Third Edition 63