Lesson 3 - Variables, Constants and Calculations
Lesson 3 - Variables, Constants and Calculations
Lesson 3 - Variables, Constants and Calculations
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
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
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.
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)
Product
Difference
Quotient