visualbasic lecture5.pdf
TRANSCRIPT
-
8/10/2019 visualbasic lecture5.pdf
1/19
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
VariableNameis the programmer designated name
Asis a keyword
DataTypeis one of many possible keywords for the type
of value the variable will contain
!hapter "# $lide %
-
8/10/2019 visualbasic lecture5.pdf
2/19
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 Inte er!hapter "# $lide
-
8/10/2019 visualbasic lecture5.pdf
3/19
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 "
-
8/10/2019 visualbasic lecture5.pdf
4/19
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 thevariable name should be capitali/ed
intours0orked # an integer variable
str1astame # a string (or text) variable !hapter "# $lide 2
-
8/10/2019 visualbasic lecture5.pdf
5/19
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 txtame to thevariable greeting
greeting = "Gd !rning " t#tName$Te#t
An assignment changes only the left operand!hapter "# $lide 5
-
8/10/2019 visualbasic lecture5.pdf
6/19
Copyright 2011 Pearson Addison-Wesley
Visual -asic Data Types
6nteger types
-yte
$hort
6nteger
1ong
7loating#8oint types $ingle
Double
Decimal !hapter "# $lide 9
-
8/10/2019 visualbasic lecture5.pdf
7/19
Copyright 2011 Pearson Addison-Wesley
umber $ystem
0hat number system do we use
De%imal
6t;s called decimal because it has 1&symbols
-
8/10/2019 visualbasic lecture5.pdf
8/19
Copyright 2011 Pearson Addison-Wesley
umber $ystem (cont@)
0hat is the value of this number %
-
8/10/2019 visualbasic lecture5.pdf
9/19
Copyright 2011 Pearson Addison-Wesley
umber $ystem (cont@)
% -yte > bits
nsigned
%B=C%B9C%B5C%B2C%B"C%BC%B%C%B
C 92 C " C %9 C > C 2 C C % 55
$igned!hapter "# $lide ?
8osition: = 9 5 2 " % , range is
& t 2(( )2* +1=2((
$igned
#n#% to C(n#% #%), n number of bits
6f n >, range is
+ +!hapter "# $lide%",92> to
,%2=,2>",92=
1ong lng $igned integer from #?,","=,52,==5,>
-
8/10/2019 visualbasic lecture5.pdf
12/19
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
8refixDescription
$ingle sng As large as %
-
8/10/2019 visualbasic lecture5.pdf
13/19
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!hapter "# $lide%"
-
8/10/2019 visualbasic lecture5.pdf
14/19
Copyright 2011 Pearson Addison-Wesley
The $tring Data Type
A string literal is enclosed in +uotation
marks
The following code assigns the nameGose 4on/ales to the variable strame
Dim strName as string
strName = "/se Gn0ales"
An empty string literal can be coded as:
Two consecutive +uotation marks!hapter "# $lide%2
-
8/10/2019 visualbasic lecture5.pdf
15/19
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!hapter "# $lide%5
-
8/10/2019 visualbasic lecture5.pdf
16/19
Copyright 2011 Pearson Addison-Wesley
Declaring Variables with
6ntelli$ense As you enter your program, V- often aids youby 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!hapter "# $lide%9
-
8/10/2019 visualbasic lecture5.pdf
17/19
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 %:
-
8/10/2019 visualbasic lecture5.pdf
18/19
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
sually want to set an initial value unlessassigning a value prior to using the variable
Gust append = :al;eto the Dim statement
where value is the literal to be assigned to thevariable
Dim int!nths7er
-
8/10/2019 visualbasic lecture5.pdf
19/19
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 thesecharacteristics
$cope begins where variable is declared
.xtends to end of procedure where declared!hapter "# $lide%?