Copyright 2011 Pearson Addison-Wesley
Declaring Variables
A variable declaration is a statement that creates a variable in
memory
The syntax is:
Dim VariableName As DataType
Dim (short for Dimension) is a keyword
VariableName is the programmer designated name
As is a keyword
DataType is one of many possible keywords for the type
of value the variable will contain
ere is an example of a variable declaration:
Dim intLength as Integer
!hapter "# $lide %
Copyright 2011 Pearson Addison-Wesley
Declaring &ultiple Variables
$everal variables may be declared in one
statement if they all hold the same type of
value
Dim intLength, intWidth, intHeight as Integer
'r this can be done in " separate statements
Dim intLength as Integer
Dim intWidth as Integer
Dim intHeight as Integer
!hapter "# $lide (
Copyright 2011 Pearson Addison-Wesley
Variable )aming *ules
The first character of a variable name must be
a letter or an underscore
$ubse+uent characters may be a letter,
underscore, or digit
Thus variable names cannot contain spaces or
periods (or many other kinds of characters)
Visual -asic keywords cannot be used as
variable names
!hapter "# $lide "
Copyright 2011 Pearson Addison-Wesley
Variable )aming !onventions
)aming conventions are a guideline to help
improve readability but not re+uired syntax
A variable name should describe its use
.ach data type has a recommended prefix, in
lower case, that begins the variable name
The %st letter of each subse+uent word in the
variable name should be capitali/ed
intours0orked # an integer variable
str1ast)ame # a string (or text) variable
!hapter "# $lide 2
Copyright 2011 Pearson Addison-Wesley
$etting the Value of a Variable
An assignment statement is used to set the
value of a variable, as in:
Assign the value %%( to the variable length
length = 112
Assign the string literal 34ood &orning 3 followed
by the contents of the text box txt)ame to the
variable greeting
greeting = "Gd !rning " " t#tName$Te#t
An assignment changes only the left operand
The right operand remains unchanged
!hapter "# $lide 5
Copyright 2011 Pearson Addison-Wesley
Visual -asic Data Types
6nteger types
-yte
$hort
6nteger
1ong
7loating#8oint types
$ingle
Double
Decimal
'ther data types
-oolean
!har
$tring
Date
!hapter "# $lide 9
Copyright 2011 Pearson Addison-Wesley
)umber $ystem
0hat number system do we use:
De%imal
6t;s called decimal because it has 1& symbols
<, %, (, ", 2, 5, 9, =, >, ?
0hat number system does computer use:
'inary
6t;s called binary because it has 2 symbols
<, %
!hapter "# $lide =
Copyright 2011 Pearson Addison-Wesley
)umber $ystem (cont@)
0hat is the value of this number %<<<%
Decimal: %<<<% and -inary: %=
Decimal A %B%<2C<B%<"C<B%<(C<B%<%C%B%<<
A %<<<< C < C < C < C % A %<<<%
-inaryA %B(2C<B("C<B((C<B(%C%B(<
A %9 C < C < C < C % A %=
!hapter "# $lide >
8osition: 2 " ( % <
Value: % < < < %
Copyright 2011 Pearson Addison-Wesley
)umber $ystem (cont@)
% -yte A > bits
Dnsigned
%B(=C%B(9C%B(5C%B(2C%B("C%B((C%B(%C%B(
<
A %(> C 92 C "( C %9 C > C 2 C ( C %A (55
$igned
#%B(=C%B(9C%B(5C%B(2C%B("C%B((C%B(%C%B(<
A #%(> C 92 C "( C %9 C > C 2 C ( C %A #%
!hapter "# $lide ?
8osition: = 9 5 2 " ( % <
Value: % % % % % % % %
Copyright 2011 Pearson Addison-Wesley
)umber $ystem (cont@)
)umber *ange
Dnsigned
< to ((n #%), n A number of bits
6f n A >, range is
& t 2(( )2* +1=2((,
$igned
#(n#% to C((n#% #%), n A number of bits
6f n A >, range is
+12* )2-=12*, t .12- )2- +1=12-,
!hapter "# $lide
%<
Copyright 2011 Pearson Addison-Wesley
6nteger Data Types
7or values that will always be a whole number
Dsually name a variable starting with a " or 2
letter prefix indicating the variable;s type
!hapter "# $lide
%%
Data Type
)aming
8refix
Description
-yte byt Dnsigned integer from < to (55
$hort shrt $igned integer from #"(,=9> to "(,=9=
6nteger int $igned integer from #(,%2=,2>",92> to
(,%2=,2>",92=
1ong lng $igned integer from #?,((","=(,<"9,>52,==5,><>
to ?,((","=(,<"9,>52,==5,><=
Copyright 2011 Pearson Addison-Wesley
7loating#8oint Data Types
7or values that may have fractional parts
$ingle used most fre+uently
Double sometimes used in scientific calculations
Decimal often used in financial calculations
!hapter "# $lide
%(
Data
Type
)aming
8refix
Description
$ingle sng As large as %<"> plus or minus, = decimal positions
Double dbl As large as %<"<> plus or minus,%5 decimal positions
Decimal dec As large as %<(? plus or minus, (? decimal positions
Copyright 2011 Pearson Addison-Wesley
'ther !ommon Data Types
-oolean E variable naming prefix is bln
olds ( possible values, True or 7alse
!har E variable naming prefix is chr
olds a single character
Allows for characters from other languages
$tring E variable naming prefix is str
olds a se+uence of up to ( billion characters
Date E variable naming prefix is dat or dtm
!an hold date andFor time information
!hapter "# $lide
%"
Copyright 2011 Pearson Addison-Wesley
The $tring Data Type
A string literal is enclosed in +uotation
marks
The following code assigns the name
Gose 4on/ales to the variable str)ame
Dim strName as string
strName = "/se Gn0ales"
An empty string literal can be coded as:
Two consecutive +uotation marks
strName = ""
'r by the special identifier $tring@.mpty
strName = 1tring$2mpty
!hapter "# $lide
%2
Copyright 2011 Pearson Addison-Wesley
The Date Data Type
Date data type variables can hold the date and time
or both
Hou can assign a date literal to a Date variable, as
shown here:
Dim dtm'irth As Date
dtm'irth = 3(4142&1&3
A date literal is enclosed within I symbols
All of the following Date literals are valid:
31241&42&1&3
3*56(5&& 7!3
31&42&42&1& 859&5&& A!3
!hapter "# $lide
%5
Copyright 2011 Pearson Addison-Wesley
Declaring Variables with
6ntelli$ense
As you enter your program, V- often aids you
by offering a list of choices that could be used
at that point
After typing JAsJ in a variable declaration, V-
will offer an alphabetical list of all possible
data types
Type the first few letters of the data type name
6ntelli$ense box will highlight the matching type
8ress the Tab key to select highlighted choice
'r Kust complete typing the entire data type
name
!hapter "# $lide
%9
Copyright 2011 Pearson Addison-Wesley
Default Values and 6nitiali/ation
0hen a variable is first created in memory, it
is assigned a default value
numeric types are given a value of /ero
-oolean types are given a value of 7alse
strings are given a value of )othing
dates default to %(:<<:<< A& Ganuary %,%
4ood practice to initiali/e string variables
Dim strName as 1tring = 1tring$2mpty
$tring with value )othing causes error if used
!hapter "# $lide
%=
Copyright 2011 Pearson Addison-Wesley
6nitiali/ation of Variables
!an provide a starting or initiali/ation value for
any type of variable in a Dim statement
Dsually want to set an initial value unless
assigning a value prior to using the variable
Gust append = :al;e to the Dim statement
where value is the literal to be assigned to the
variable
Dim int!nths7er<ear As Integer = 12
!hapter "# $lide
%>
Copyright 2011 Pearson Addison-Wesley
$cope and 1ocal Variables
$cope refers to the part of the program
where:
A variable is visible and
&ay be accessed by program code
Variables declared within a procedure are
called local variables and observe these
characteristics
$cope begins where variable is declared
.xtends to end of procedure where declared
Variable is not visible outside the procedure
A variable cannot be declared twice in the
same procedure
!hapter "# $lide
%?