VB Programming Language
VARIABLES, CONSTANTS & CALCULATIONS
BASIC (Beginners All-Purpose Symbolic Instruction)
Programs all about manipulating data
Data storage
Data manipulation
Variables and Expressions
Variables
a programming element used to store a value in the
program while the program is running
Every variable you create has a name
A variable is analogous to a box in which you can
store information and then retrieve the same
information while the program is running.
Variables
Unlike variables do not have properties or respond
to events.
Every variable has a date type that determines the
kind of information it can store.
Each data type has an associated prefix
Every variable consumes a physical space in the
computer’s memory.
Variables
is a named entity that represents some unknown
value
It has both name and contents
Example
y = x2 +
2x + 1 or y = x*x+2*x+1
Must begin with a letter (a-z, A-Z)
Cannot contain periods
Must be unique
Can contain up to 255 characters
Variable Declaration
Declaration = creation
Use DIM statement to declare a variable
Syntax:
Dim <varname> As <Type>
Examples:
Dim Age As Integer
Dim X As Double
Dim LastName As string
Dim Saved As Boolean
Declaration Examples
3- 7
Dim customerNameString As String
Private totalSoldInteger As Integer
Dim temperatureSingle As Single
Dim priceDecimal As Decimal
Private priceDecimal As Decimal
Visual Basic Data Types:
Data
DataType
Type Storage
StorageSize
Size Prefix
Prefix Possible
PossibleValues
Values
Date
Date 88Bytes
Bytes dat
dat Dates
Datesbetween
between1/1/100
1/1/100and
and12/31/9999
12/31/9999
Integer
Integer 22Bytes
Bytes int
int Positive
Positiveand
andNegative
NegativeWhole
WholeNumbers
Numbers
Long
LongInteger
Integer 44Bytes
Bytes lng
lng Positive
Positiveand
andNegative
NegativeWhole
WholeNumbers*
Numbers*
Single
Single 44Bytes
Bytes sng
sng AAnumber
numberwith
withaadecimal
decimalpoint*
point*
Double
Double 88Bytes
Bytes dbl
dbl AAnumber
numberwith
withaadecimal
decimalpoint*
point*
String
String 11Bytes
Bytes** Str
Str Up
Uptoto22billion
billioncharacter
character for
for var
var strings
strings
Boolean
Boolean 11Bytes
Bytes** Str
Str True
Trueor
or False
False
char
char
Variable Naming Conventions
Variables
VariablesNames
NamesMust
Must Variables
VariablesNames
NamesShould
Should
Begin with a four-char prefix indicating the scope
Begin with a four-char prefix indicating the scope
Begin
Beginwith
withaaletter
letter and type of data stored in the variable, followed
and type of data stored in the variable, followed
by a descriptive name.
by a descriptive name.
Always
Alwaysuseuseaacapital
capitalletter
letterasasthe
thefirst
firstcharacter
character
Not
Notcontain
containperiod,
period, ofofeach
each word in the descriptive portion ofthe
word in the descriptive portion of the
dashes,
dashes,ororspaces.
spaces. name.
name.
Not
Notexceed
exceed255255
Characters in length.
Characters in length.
Be
Beunique
unique
Declaring a Variable
The process of creating a variable is known
as declaring a variable
To declare a Visual Basic variable you may
use:
o Visual Basic Private Statement
o Visual Basic Public Statement
o Visual Basic Dim Statement
Procedural Level Variables
Declared within a procedure
Local variables
Use the Dim keyword
Examples:
Dim MyInt As Integer
Dim Mydouble As Double
Dim MyString, YourString As String
Form Level Variables
Available to all procedures defined within the form
Examples:
Option Explicit
Dim MyInt As Integer
Dim Mydouble As Double
Dim MyString, YourString As String
Global Level Variables
Available to any form and modules inside the project
Must be declared in a module
Use the Global keyword instead of Dim
Examples:
Global myAge As Integer
Global myName As String
Global mySalary As Double
Declaring Variables:
Syntax [Private][Public][Dim] Varname As Type
Definition The Private keyword declares a variable, meaning that
declared with the Private keyword, the variable
variable can
can be
be
the memory for the variable is allocated. When
used only in the module (form) in which
which itit is
is declared
declared
and by the procedures in that modules.
modules.
The Public keyword creates a variable that can be user
by multiple modules.
The Dim keyword,
keyword, depending
depending on
on where
where itit is used,
used,
creates a variable that can be used by the procedure
procedure in in
which is is declared and by other procedures
procedures
Declaring Variables:
Syntax [Private][Public][Dim] Varname As Type
The
The Varname is the name by which the variable
variable isis
Definition
known.
known.
The
The Type is the data type of the variable.
When
When you
you use
use a Private,
Private, Public,
Public, or
or Dim
Dim statement
statement
to
to declare
declare a variable,
variable, you
you explicitly
explicitly declare the
the variable.
variable.
IfIf you
you use a variable name in a Dim statement, Visual
Basic
Basic will create
create the
the variable
variable automatically
automatically in a process
process
known
known as implicit
implicit declaration.
declaration.
Declaring Variables:
Syntax Option Explicit
The Option
Option Explicit
Explicit statement
statement forces
forces you
you to
to declare
declare all
all
Definition
Variables
Variables explicitly
explicitly with the Private, Public or Dim
keyword
keyword in
in the module
module where
where the
the Option
Option Explicit
Explicit
statement
statement appears.
appears.
The Option explicit statement must be the first
statement
statement in
in the
the general declarations
declarations section
section of the
module
module
Operators in VB
Operator(s) operation Order of evaluation(precedence)
^ Exponentiation Evaluated first. If there are several such operators,
they’re evaluated from left to right.
+, - Sign operations Evaluated second. If there are several such operators,
(unary) they’re evaluated from left to right.
*, / Multiplication Evaluated third. If there are several such operators,
and division they’re evaluated from left to right.
\ Integer division Evaluated fourth. If there are several such operators,
they’re evaluated from left to right.
Mod Modulus Evaluated fifth. If there are several such operators,
they’re evaluated from left to right.
+, - Addition and Evaluated sixth. If there are several such operators,
subtraction they’re evaluated from left to right.
(binary)
VB OPERATION ARITHMETIC ALGEBRAIC VB EXPRESSION
OPERATOR EXPRESSION
ADDITION + F+7 f+7
SUBTRACTION - P-c p- c
MULTIPLICATION * Bm b*m
DIVISION / X /y x/y
DIVISION \ none V\u
MODULUS MOD r mod s r mod s
EXPONENTIATION ^ qp q^p
UNARY MINUS - -e -e
UNARY PLUS + +g +g
Mathematical Examples
3- 19
Note the use of parentheses to control order of
precedence
1. 3 + 4 * 2 = 11 Multiply then add
2. (3 + 4) * 2 = 14 Parentheses control: add then multiply
3. 8 / 4 * 2 = 4 Same level, left to right: divide then multiply
Using Expression and Operators to Manipulate
Variables
Operators
OperatorsininOrder
Orderofof Description Example
Precedence Description Example
Precedence
Raises
RaisesaaNumber
Numbertotothe
thePower
Powerofof
^^ An
AnExponent
Exponent
22^^33isisequal
equaltoto88
*,*, // Multiplication
Multiplicationand
andDivision
Division 22**33isisequal
equaltoto66
\\ Integer
IntegerDivision
Division 10
10\\33isisequal
equaltoto33
Modulus
ModulusArithmetic;
Arithmetic;Returns
Returnsthe
theInteger
Mod
Mod Remainder
RemainderofofaaDivision
DivisionOperation
Integer
Operation
10
10mod
mod33isisequal
equaltoto11
+,
+, -- Addition
Additionand
andSubtraction
Subtraction 22++33isisequal
equaltoto55
The result of a comparison operator is a
Boolean value (T/F)
Operator Operation Meaning
NOT Logical NOT Negates an operand
AND Logical AND Returns TRUE if both
operands are TRUE else
returns FALSE
OR Logical OR Return TRUE if at least,
one of the operands is
TRUE
Operator Comparison
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
Type Conversion Functions
Visual Basic included intrinsic functions.
Intrinsic functions differ from event
procedure in that you must explicitly write a
Visual Basic statement to call them.
Our Island Financial program includes the:
Val, Format, and Future Value (FV) functions
Solving Programming Errors
Error are classified in three types:
Syntax Errors
Run-Time Errors
Logic Errors
Activity
Modify the previous program so that the
program can determine the following:
Sum
Product
Difference
Quotient