Professional Documents
Culture Documents
CHAPTER 2 - Variable, Constants and Calculations
CHAPTER 2 - Variable, Constants and Calculations
CHAPTER 2 - Variable, Constants and Calculations
2
Variables vs Constants
Variables Constants
Memory locations that hold data that CAN be Memory locations that hold data that CANNOT be
changed during project execution. changed during project execution.
Dim strname As String Const curDiscountRate As
Dim intCounter As Integer Currency = .15
Dim sngTemperature As Const strMessage As String =
Single “VB is a lot of fun!”
2 categories of constants:
i.Named constant: constants that you define
ii.Intrinsic constant: constants that are built into
VB (system-defined) and do not have to declare.
E.g: vbBlue, vbRed, vbGreen.
The reserved word Dim is really short for dimension, which means “size”
3
Data Type
7
Naming Conventions
curTaxRate
A lowercase prefix indicates
data type of currency
8
Common Type of Prefix
• Here is a list of the most common data types and their prefixes:
Prefix Data Types
bln Boolean
cur Currency
dbl Double-
precision
floating point
dtm Date/time
int Integer
lng Long integer
sng Single-precision
floating-point
str String
vnt Variant
9
Check Point 1
1. int#sold
2. i Number Sold
3. int.Number.Sold
4. Sub
5. strSub
6. Caption
7. conMaximum
8. MinimumRate
9. curMaximumCheck
10. strCompanyName
10
Assigning Value to Constant
14
Local Declaration
15
Module-Level Declaration
16
Check Point 3
17
Calculation
18
The Val Function
• The Val function converts an argument to numeric, begins at the left most
character.
• If that character is a numeric digit, decimal point or sign, Val converts the
character to numeric and moves to the next character.
• As soon as a nonnumeric character is found, the operation stops.
• Eg:
Contents of Numeric Value Returned by Contents of Numeric Value Returned by
Argument the Val Function Argument the Val Function
(blank) 0 123A 123
123.45 123.45 4B5 4
$100 0 -123 -123
1,000 1 +123 +123
19
A123 0 12.34.8 12.34
Arithmetic Operations
• The order of precedence:
1. Parentheses
Operator Operation 2. Exponentiation
+ Addition 3. Multiplication and division
- Subtraction 4. Addition and subtraction
* Multiplication • The following expressions would
/ Division be valid in maths, but not valid in
^ Exponentiation
Basic:
Mathematical Equivalent Basic
Notation Function
2A 2*A
3(X+Y) 3*(X+Y)
(X+Y)(X-Y) (X+Y)*(X-Y)
20
Check Point 4
22
Formatting Data
23
FormatCurrency Function
FormatCurrency(NumericExpressionToFormat)
24
FormatNumber Function
FormatNumber(ExpressionToFormat)
25
FormatPercent Function
FormatPercent(ExpressionToFormat)
FormatDateTime(ExpressionToFormat, NamedFormat)
• To format an expression as a date and/or time.
• The expression may be a string that holds a date or time value, or a data type variable. Eg:
• Give the line of code that assigns the formatted output and tell
how the output will display for the specified value.
1. A calculated variable called mcurAveragePay has a value of 123.456 and
should display in a label called lblAveragePay.
2. The variable sngCorrect, which contains .76, must be displayed as a
percentage in the labele called lblPercentCorrect.
3. The total amount collected in a fund drive is being accumulated in a
variable called mcurTotalCollected. What statement will display the
variable in a label called lblTotal with commas and two decimal positions
but no ringgit signs?
28
ANY QUESTION…?
29