1
Objectives
• Declare both a variable and a named constant
2
Objectives (continued)
• Include internal documentation in the code
3
Objectives (continued)
• Explain the difference between syntax errors and logic errors
4
Variables
• Computer memory locations where users can temporarily store data
5
Selecting a Data Type for a Variable
6
Selecting a Data Type for a Variable (continued)
• Integers whole numbers
⁻ Integer, Long, Short
• Floating-point numbers real numbers
⁻ Single, Double – use exponential notation
Example: 32000 = 3.2E4
⁻ Decimal uses fixed decimal point
• Character Types
⁻ Char – one character
⁻ String – 0 to 2 billion characters
7
Selecting a Name for a Variable
• Name should be descriptive
⁻ Type m and 3 letter prefix using lowercase, then capitalize the first letter
of each word in the variable name
8
Selecting a Name for a Variable (continued)
9
Selecting a Name for a Variable (continued)
10
Declaring a Variable
11
Declaring a Variable (continued)
• accessibility variablename as [datatype = initialvalue]
• accessibility - sets scope of variable
⁻ Dim, Public, Private
• variablename = programmer chosen name
• datatype = data type of the variable
• Anything in brackets is optional
• initialvalue = default value at start of program
⁻ If no value specified numeric types = 0, character types = Nothing,
boolean = false
12
Assigning Data to an Existing Variable
• assignment statement
⁻ strName = “Mary”
⁻ intAge = 35
“=“ sign is called assignment operator
• Literal constant – value does not change
⁻ Number 35 is numeric literal constant
⁻ “Mary” is a string literal constant
String - a group of characters enclosed in quotes
13
Assigning Data to an Existing Variable (continued)
14
Assigning Data to an Existing Variable (continued)
• Literal type characters force literal constants
to assume new data type
15
Using the Convert Class
• Convert class creates object that is either a
number or a string by converting value to a
specified data type and returning result
strAge = Convert.ToString(intAge)
16
Writing Arithmetic Expressions
• Precedence number indicates order in which
VB performs operation in an expression
17
Writing Arithmetic Expressions (continued)
• Integer division operator (\)
⁻ Returns integer portion of division discarding remainder
8\3 results in answer of 2
• Modulus arithmetic operator (Mod)
⁻ Returns remainder portion of division discarding integer portion
7 Mod 3 results in an answer of 1
Often used to determine Leap years
Valid only with division of integer values
18
How To…
19
The Scope and Lifetime of a Variable
• Scope
• Lifetime
20
The Scope and Lifetime of a Variable (continued)
• Procedure Scope
⁻ Procedure-level variable
Variable declared within a procedure
Variable can only be used in procedure in which declared
• Module scope
⁻ Module-level variable
Variable declared in General Declarations section of the form
Variable can be used within all procedures in the form
21
Procedure-level variables example
22
Module-level variable example
23
The Scope and Lifetime of a Variable (continued)
• Block scope
⁻ Block-level variable
‐ If…Then…Else or For….Next
24
Named Constants
• A memory location inside a computer whose
value cannot be changed while the application is
running
25
Named Constants (continued)
26
Internally Documenting the Program Code
• Internal documentation:
27
Internally Documenting the Program Code (continued)
• Include comments:
⁻ as necessary to explain various sections of code
⁻ at beginning of application explaining application’s:
Name
Purpose
Author
Date of creation or modification
28
Internally Documenting the Program Code (continued)
29
Option Explicit and Option Strict
• Option Explicit On
⁻ Tells computer to warn you if your code contains name of an undeclared
variable
⁻ Prevents unintentional declaration of variables which automatically are
of type Object
• Option Strict On
⁻ Tells computer not to perform any implicit type conversions which may
lead to loss of data
Implicit type conversions are automatic conversions from one data type to
another performed by the computer to fit data into an assigned memory
location
30
Option Explicit and Option Strict (continued)
31
Coding the SKATE-AWAY SALES Application
• Application calculates and displays total number of skateboards
ordered and total price of skateboards including 5% sales tax
• Planning steps:
⁻ Identify tasks the application needs to perform
⁻ Identify objects to which you will assign those tasks
⁻ Identify events required to trigger an object into performing its assigned
tasks
⁻ Design the user interface
32
Testing and Debugging the Application
• Invalid data - data the application is not expecting
• Debugging:
⁻ Process of locating syntax and logic errors in the program
Syntax errors
‐ Typographical errors that violate rules governing valid syntax of the language
‐ Me.Colse() instead of Me.Close()
Logic errors
‐ Instruction that does not give expected results
‐ decAverage = decNum1 + decNum2 / 2
33
Testing and Debugging the Application (continued)
• First test with valid data
34
Testing and Debugging the Application (continued)
• Then test with invalid data
35
Formatting Numeric Output
• Formatting:
⁻ Specifying number of decimal places and special characters to display in
a number
• Format string:
⁻ String used to specify format
Example: “Axx” must be enclosed in quotes
Format specifier - A
Alphabetic character that specifies format you wish to use
Precision specifier – xx
‐ xx is the sequence of digits desired
36
Formatting Numeric Output (continued)
37
Formatting Numeric Output (continued)
38
Formatting Numeric Output (continued)
39
Summary
• Variables and named constants are computer memory locations that
store data
• Variables are declared with a name and data type
• Scope refers to where a program can access a variable or named constant
⁻ Module scope variables can be accessed anywhere in the module
⁻ Procedure scope variables can only be accessed in the procedure in
which declared
⁻ Block scope variables can only be accessed within the block in which
declared
40
Summary (continued)
• Lifetime refers to how long a variable remains in the computer’s
memory
• Literal constants are items of data
• Convert class contains methods to convert values to a specified data
type
• Integer division operator divides 2 integers and returns the result
discarding any remainder
• Modulus division operator divides 2 integers and returns the remainder
41
Summary (continued)
• Option Explicit warns if undeclared variables are used
• Option Strict prevents implicit type conversions that may result in loss
of data
• Use pseudocode (short phrases) or flowcharts (standardized symbols) to
plan the application
• Test application with both valid and invalid data
• Format numeric output to display special characters
42