Virtual Basic Lecture 3
Virtual Basic Lecture 3
DiscountedAmount.ToString)
A variable’s name must begin with a letter
The only special character that can appear in
a variable’s name is the underscore character.
It mustn’t exceed 1,024 characters.
It must be unique within its scope.
Visual Basic recognizes the following five
categories of variables:
◆ Numeric
◆ String
◆ Boolean
◆ Date
◆ Object
Numeric : for storing numerical values which
include Integer, Decimal, Single, and Double
String : data type for storing text.
Boolean: for string True/False values.
Date : store date values that may include a
time part.
Object: variables without a fixed data type,
which can store all types of values, such as
integers, strings, characters
Variable has the capability of exposing some functionality
by means of properties and methods.
Example:
Dim expiration As Date = #1/1/2003#
Dim newExpiration As Date
newExpiration = expiration.AddYears(3)
21
Example: Loss of Precision
• Loss of precision
– Computing error that can occur when decimal
positions are dropped
Dim myInteger As Integer = 1
Dim myDouble As Double = 2.5
myInteger = myDouble
• VB .NET will automatically round decimal values
before truncating
22
Example: Loss of Precision
(continued)
• Option Strict
– Prevent unintentional loss of precision when
mixing data types in assignment statements
– Compiler detects potential loss of precision
• Displays error message
• Explicit type conversion
– Invoke Convert method to convert data types
23
24
Converting Data Types
(continued)
• Option Explicit
– Otherwise
– Generally set On
25
Using Reference Variables
• Uses class name as data type
• For example:
– String
28
Defining Constants (continued)
• Naming convention:
– Example:
• TAX_ID
29
Writing Basic Computational
Statements
• Concatenate operator
–&
– Joins two Strings
• Arithmetic operators
– For multiplication, division, addition, and
subtraction
– *, /, +, -
30
31
Order of Operations
Hierarchy of operations, or order of precedence, in
arithmetic expressions from highest to lowest
1. Any operation inside parentheses
2. Exponentiation
3. Multiplication and division
4. Integer division
5. Modulus
6. Addition and subtraction
3-
Evaluation of Expression
1. All operations within parentheses. Multiple operations within the
parentheses are performed according to the rules of precedence.
2. All exponentiation. Multiple exponentiation operations are
performed from left to right.
3. All multiplication and division. Multiple operations are performed
from left to right.
4. All integer division. Multiple operations are performed from left to
right.
5. Mod operations. Multiple operations are performed from left to
right.
6. All addition and subtraction are performed from left to right.
3-
Mathematical Examples
Note the use of parentheses to control order of precedence.
3-
Using Calculations in Code
Perform calculations in assignment statements.
What appears on right side of assignment operator is
assigned to item on left side.
Assignment operators — allows shorter versions of
code =, +=, -=, *=, /=, \=, &=
‘Accumulate a total.
TotalSalesDecimal += salesDecimal
3-
Example:
Integer Division (\)
Dim firstInt As Integer = 11
Dim secondInt As Integer = 2
Dim integerResult As Integer = 0
integerResult = firstInt \ secondInt
Console.WriteLine(“integerResult = firstInt \
secondInt: “ & integerResult)
• Sample Run:
– integerResult = firstInt \ secondInt: 5
36