CHAPTER 3

advertisement
Chapter 3
I)
Variables, Constants, and Calculations – Dim statements declare variables –
pg. 86
II)
Data—Variables and Constants  Review Questions 2, 4
A) Variables and constants are temporary memory locations that have a name
(called an identifier), a data type, and a scope. The value stored in a variable
can be changed during the execution of the project; the values stored in
constants cannot change. – pg. 86
1) Declaration statements – pg. 87
B) Data Types
 Review Question 1
1) The data type determines which values may be stored in a variable or
constant. The most common data types are string, integer, currency, single
precision, and Boolean. Any variable not explicitly given a data type
defaults to variant – less efficient – always specify data type. – pg. 87 –
most common types are string, integer, and currency – pg. 88.
C) Naming Rules: 1-255 characters, letters, digits, underscores, no spaces or
periods
D) Identifiers for variables and constants must follow the Visual Basic naming
rules and should follow good naming standards, called conventions. An
identifier should be meaningful and have a lowercase prefix that indicates
the data type and the scope. Variable and constant names should be mixed
upper- and lowercase for readability.
1) Naming Conventions – pg. 89
E) Constants—Named and Intrinsic
1) Named – define yourself using CONST – give name, data type, and
value – easier to read, and less to change – only once if referenced
numerous times in the project – pg. 90
(a) Assigning values to constants – string constants - text, quotation
marks – string literals; numeric constants – digits, sign and decimal
point only
2) Intrinsic – built into VB – system defined constants – stored in library
files
(a) Instrisic constants, such as vbRed and vbBlue, are predefined and
built-into Visual Basic. Named constants are programmer-defined
constants, and are declared using the Const statement. The location
of the Const statement determines the scope of the constant.
F) Declaring Variables
 Review Question 7
1) Variables are declared using the Dim statement (dimension - size) –
determines the size of the variable; the location of the statement
Variables, Constants, and Calculations
21
determines the scope of the variable – how much of the project it can be
used with. – pg. 93
G) Scope of Variables
 Review Question 3
1) The scope of a variable may be global, module level, or local. Local
variables are available only within the procedure in which they are
declared; module-level variables are accessible in all procedures within a
form; global variables are available in all procedures of all modules in a
project with multiple modules.
(a) Lifetime – period of time that the variable exists – if for a procedure,
when the procedure is exited the variable disappears
(b) Local declarations – inside a procedure – only for that procedure –
pg. 94-95.
(c) Module-level declarations – used anywhere – General Declarations
section – pg. 95-96.
(d) Including the scope in identifiers – to help know the scope –
documentation only – pg. 96 – pg. 96.
III) Calculations – variables and/or constants; strings changed to numbers or
numbers
1) Calculations may be performed using the values of numeric variables,
constants, and the properties of controls. The result of a calculation may
be assigned to a numeric variable or to the property of a control.
B) The Val Function
 Review Question 5
1) A Visual Basic function performs an action and returns a value. The
expressions named in parentheses are called arguments.
IV) Use the Val function to convert text values to numeric before performing any
calculations – pg. 97, converts from the leftmost character until a nonnumeric
character is found – pg. 98
V)
Arithmetic Operations
A) Order of Operations – order of precedence  Review Question 6 – pg. 99
1) A calculation operation with more than one operator follows the order of
precedence in determining the result of the calculation (*,/ before +,-).
Parentheses alter the order of operations.
B) Using Calculations in Code – right side is assigned to the left – left side
must be a variable or the property of a control, cannot be a constant – pg.
100
VI) Formatting Data – the way they look  Review Questions 8, 9
A) The formatting functions, FormatCurrency, FormatNumber, FormatPercent,
and FormatDateTime, can be used specify the appearance of values for display
– pg. 101.
1) Formatted values cannot be used in calculations.
2) Examples - pg. 102-103
22
Chapter 3
VII) A Calculation Programming Example – pg. 104-109
VIII) Counting and Accumulating Sums
A) Summing numbers - You can calculate a sum by adding each transaction to
a module-level variable. In a similar fashion, you can calculate a count by
adding to a module-level variable.
1) Summing Numbers
2) Counting – pg. 109
3) Calculating an Average – divide the sum by the count of the items – pg.
110
IX)
X)
Hands-on programming example – pg. 110
Programming Hints
A) Option explicit – to avoid common program coding errors – tools/options/
editor tab – require variable declaration – forces the programmer to declare
all variables - A frequent cause of errors in Basic programming is the
misspelling of variable names. You can avoid this error by including the
Option Explicit statement in the General Declarations section of each
module. VB will automatically add the Option Explicit statement to all new
modules if you set the option to Require Variable Declaration.
Variables, Constants, and Calculations
23
Download