VB Script
VB Script
Subjects for
Subjects for
Session
Session
Vbscript fundamentals. Vbscript fundamentals. Variant subtypes. Variant subtypes. Variables. Variables. Option ExplicitOption Explicit statement. statement. Keywords.
Keywords.
Scope and liftime of variables. Scope and liftime of variables. Dim
Dim,, PrivatePrivate and and PublicPublic statements. statements. Rem
What is VBScript?
What is VBScript?
•
•
A Safe subset of visual basic
A Safe subset of visual basic
••
The VBSc
The VBSc
ript ode is case !n
ript ode is case !n
sensitive.
sensitive.
••
"icrosoft Visual Basic Scriptin# $dition brin#s
"icrosoft Visual Basic Scriptin# $dition brin#s
active scriptin# to a
active scriptin# to a
wide variety of environments,
wide variety of environments,
includin# Web client scriptin# in "icrosoft !nternet
includin# Web client scriptin# in "icrosoft !nternet
$%plorer and Web server scriptin# in "icrosoft
$%plorer and Web server scriptin# in "icrosoft
!nternet !nformation Service &!!S'.
"icrosoft
"icrosoft
((Script Timeline
Script Timeline
!nternet !nternet $%plorer ).* $%plorer ).* +- +- Version Version /.* /.* Version Version 0.* 0.* !!S 0 !!S 0 /0- /0- !nternet !nternet $%plorer 1.* $%plorer 1.* +-2 +-2 Version Version ).* ).* Windows Windows -+ -+ Version Version )./ )./ Version Version 1.* 1.* Version Version 3.* 3.* Visual Visual Studio
Studio $%plorer 3.*$%plorer 3.*!nternet!nternet Win 4T 3.* Win 4T 3.*
4ote5 6uic7Test uses VBScript 3.
Scriptin# for Speci8c
Scriptin# for Speci8c
9latfor
9latfor
ms
ms
•
•
VBScript
VBScript
:
:
desi#ned to run on
desi#ned to run on
multiple platfor
multiple platfor
ms that use the
ms that use the
Active; Scriptin# !nterface.
Active; Scriptin# !nterface.
•
•
VBA &VB for Applications'
VBA &VB for Applications'
:
:
"ore sophisticated customi<ation.
"ore sophisticated customi<ation.
:
:
9ow
9ow
er user
er user
focus, natural up#rade, superb inte#ration
focus, natural up#rade, superb inte#ration
•
•
Visual Basic
Visual Basic
:
:
Best tool for
Best tool for
distributed clientServer solutions.
distributed clientServer solutions.
:
•
• VBScript has only one data type called aVBScript has only one data type called a VariantVariant..
•
• AA VariantVariant is a special 7ind of data type that can is a special 7ind of data type that can
contain di=erent 7inds of
contain di=erent 7inds of information, dependin# oninformation, dependin# on how it is used.
how it is used.
•
• BecauseBecause VariantVariant is the only data is the only data type in VBScript, ittype in VBScript, it
is also the data
is also the data type returned by all functions intype returned by all functions in VBScript.
VBScript.
•
• At its simplest, aAt its simplest, a VariantVariant can contain either numeric can contain either numeric
or strin# information. or strin# information.
•
• AA VariantVariant behaves as a number when you use it in a behaves as a number when you use it in a
numeric conte%t and as a strin# when you use it in a numeric conte%t and as a strin# when you use it in a strin# conte%t.
VBScript >ata Types
VBScript >ata Types
V
V
ariant
ariant
Subtypes
Subtypes
•
• Beyond the simple numeric or strin# classi8cations, aBeyond the simple numeric or strin# classi8cations, a
Variant
Variant can ma7e further distinctions about the can ma7e further distinctions about the speci8c nature of numeric information.
speci8c nature of numeric information.
•
• or e%amplor e%ample, you can hae, you can have numeric information thatve numeric information that
repre
represents a date or sents a date or a time.a time.
•
• When used with other date or When used with other date or time data, the result istime data, the result is
always e%pressed as a date or a t
always e%pressed as a date or a time.ime.
•
• @ @ou can also have a rich variety of numeric informou can also have a rich variety of numeric informationation
ran#in# in si<e from Boolean values to h
ran#in# in si<e from Boolean values to hu#e oatin#u#e oatin# point numbers.
VBScript >ata Types
VBScript >ata Types
V
V
ariant
ariant
Subtypes
Subtypes
Su
Subtbtypype e DeDescscririptptioionn $mpty
$mpty VariantVariant is uninitiali<ed. Value is * is uninitiali<ed. Value is * for numeric variables or a <erolen#th strin# &CC' for strin# for numeric variables or a <erolen#th strin# &CC' for strin# variables.variables.
4ull
4ull VariantVariant intentionally contains no valid intentionally contains no valid data.data. Boolean
Boolean ontains eitherontains eitherTrueTrue or orFalseFalse.. Byte
Byte ontains inte#er in the ran#e * to 033.ontains inte#er in the ran#e * to 033. !nte#er
!nte#er ontains inte#er in the ran#e )0,2+ to ontains inte#er in the ran#e )0,2+ to )0,22.)0,22. urrency
urrency -00,))2,0*),+3,122.-00,))2,0*),+3,122.3+*+ 3+*+ to to -00,))2,0*),+3,122.3+*-00,))2,0*),+3,122.3+*2.2. Don#
Don# ontains inte#er in the ran#e 0,/12,1+),1+ to 0,/12,1+),12.ontains inte#er in the ran#e 0,/12,1+),1+ to 0,/12,1+),12. Sin#le
Sin#le ontains a sin#leprecision, oatin#point number in the ontains a sin#leprecision, oatin#point number in the ran#e ).1*0+0)$)+ to /.1*/0-+$13 forran#e ).1*0+0)$)+ to /.1*/0-+$13 for ne#ative valuesE /.1*/0-+$13 to ).1*0+0)$)+ for
ne#ative valuesE /.1*/0-+$13 to ).1*0+0)$)+ for positive values.positive values. >ouble
>ouble ontains a doubleprecision, oatin#point number in the ontains a doubleprecision, oatin#point number in the ran#e /.2-2-)/)1+0)0$)*+ toran#e /.2-2-)/)1+0)0$)*+ to 1.-1*313+1/012$
1.-1*313+1/012$)01 for )01 for ne#ative valuesE 1.-1*313+1/012$)01 to /.2-2-)/)1+0)0$)*+ forne#ative valuesE 1.-1*313+1/012$)01 to /.2-2-)/)1+0)0$)*+ for positive values.
positive values.
>ate
>ate ontains a number that represents a date between Fanuary /, /** to >ecember )/, ----.ontains a number that represents a date between Fanuary /, /** to >ecember )/, ----. Strin#
Strin# ontains a variablelen#th strin# that can be up ontains a variablelen#th strin# that can be up to appro%imateto appro%imately 0 billy 0 billion characters in len#th.lion characters in len#th. GbHect
Variables
Variables
•
•
A variable is a convenient placeholder that refers to a
A variable is a convenient placeholder that refers to a
computer memory location where you can store
computer memory location where you can store
pro#ram inform
pro#ram information that may
ation that may chan#e durin# the
chan#e durin# the
time your script is runnin#.
time your script is runnin#.
•
•
or e%am
or e%ample, you mi#ht create a
ple, you mi#ht create a
variable called
variable called
lic7ount to store the number of times a user clic7s
lic7ount to store the number of times a user clic7s
an obHect on a particular Web pa#e.
an obHect on a particular Web pa#e.
•
•
you only have to refer to a variable by his name to
you only have to refer to a variable by his name to
see or chan#e its value.
see or chan#e its value.
>eclarin
>eclarin
# V
# V
ariables
ariables
•
•
@
@
ou declar
ou declar
e variables e%p
e variables e%p
licitly in your scri
licitly in your scri
pt usin# the
pt usin# the
Dim
Dim
statement, the
statement, the
Public
Public
statement, and the
statement, and the
Private
Private
statement. or e%ample5
statement. or e%ample5
Dim
Dim
>e#rees
>e#rees
ahrenheit
ahrenheit
••
@
@
ou declar
ou declar
e multiple variables
e multiple variables
by separatin# eac
by separatin# eac
h
h
variable name with a comma. or e%ample5
variable name with a comma. or e%ample5
Dim
>eclarin
>eclarin
# V
# V
ariables
ariables
•
•
@
@
ou can also declar
ou can also declare a variable impli
e a variable impli
citly by
citly by
simply usin# its name in your script.
simply usin# its name in your script.
•
•
That is not #eneral
That is not #enerally a #ood practice becau
ly a #ood practice because
se
you could misspell the variable name in one or
you could misspell the variable name in one or
more places, causin# une%pected results when
more places, causin# une%pected results when
your script is run.
your script is run.
•
•
or that reason, the
or that reason, the
Option Explicit
Option Explicit
statement is available to reJuire e%plicit
statement is available to reJuire e%plicit
declaration of all variables.
Gption $%plicit Statement
Gption $%plicit Statement
•
•
or
or
ces e%plicit declaration of
ces e%plicit declaration of all variables in
all variables in a script.
a script.
•
•
!f used, the
!f used, the
Option
Option
Explicit
Explicit
statement must appear
statement must appear
in a script before any
in a script before any other statements.
other statements.
•
•
When you use the
When you use the
Option
Option
Explicit
Explicit
statement, you
statement, you
must e%plicitly declare all variables usin# the
must e%plicitly declare all variables usin# the
Dim
Dim
,
,
Private
Private
,
,
Public
Public
, or
, or
ReDim
ReDim
statements. !f you
statements. !f you
attempt to use an
attempt to use an undeclared variable name, an error
undeclared variable name, an error
occurs.
occurs.
•
•
Tip
Tip
se
se
Option
Option
Explicit
Explicit
to avoid incorrectly typin#
to avoid incorrectly typin#
the name of
the name of an e%istin# variable or to avoid confusion
an e%istin# variable or to avoid confusion
in code where the scope of the variable is not clear.
in code where the scope of the variable is not clear.
•
•
"ust be#in with an alphabetic character.
"ust be#in with an alphabetic character.
••
annot contain an
annot contain an
embedded period.
embedded period.
••
"ust not
"ust not
e%
e%
ceed 033 characters.
ceed 033 characters.
••
"ust be uniJue in the scope in which it is
"ust be uniJue in the scope in which it is
declared.
declared.
Tip
Tip
meanin#full pre
meanin#full pre
8% to
8% to
variables to indicate t
variables to indicate t
he
he
subtypes i.e
subtypes i.e
iounter &inte#er', str4ame &Strin#',
iounter &inte#er', str4ame &Strin#',
bla# &Boolean',
bla# &Boolean',
dteToday &>ate'
•
•
$mpty
$mpty
:
: The The EmptyEmpty 7eyword is used to indicate an uninitiali<ed 7eyword is used to indicate an uninitiali<ed
variable value.
variable value.
•
•
4ull
4ull
:
: The The NullNull 7eyword is used to indicate that a variable contains 7eyword is used to indicate that a variable contains
no valid data.
no valid data.
•
•
T
T
rue
rue
:
: The The TrueTrue 7eyword has a value eJual to /. 7eyword has a value eJual to /.
•
•
alse
alse
:
: The The FalseFalse 7eyword has a value eJual to *. 7eyword has a value eJual to *.
•
•
4othin#
4othin#
:
: The The NothinNothin 7eyword in VBScript is used to disassociate an 7eyword in VBScript is used to disassociate an
obHect variable from any actual obHect.
•
• A variableLs scope is determined by where you declare it.A variableLs scope is determined by where you declare it.
•
• When you declare a variable within When you declare a variable within a procedure, only code within thata procedure, only code within that
procedur
procedure can access or e can access or chan#e the value of tchan#e the value of that variable.hat variable.
•
• !f you declare a !f you declare a variable outside a variable outside a procedurprocedure, you ma7e it reco#ni<able to alle, you ma7e it reco#ni<able to all
the procedures in your script. the procedures in your script.
•
• The lifetime of a variable dep The lifetime of a variable depends on how lon# it eends on how lon# it e%ists.%ists.
•
• The lifetime of a scriptlevel The lifetime of a scriptlevel variable e%tends frvariable e%tends from the time it is dom the time it is declaredeclared
until the time the script is
until the time the script is 8nished runnin#.8nished runnin#.
•
• At procedurAt procedure level, a variable e%ists only as e level, a variable e%ists only as lon# as you are in tlon# as you are in the procedurehe procedure..
•
• ou can have ou can have local variables of the same local variables of the same name in several di=erent proceduresname in several di=erent procedures
because each is
•
• DimDim varnameM varnameM!!MsubscriptsNMsubscriptsN""NMNM## varnameMvarnameM!!MsubscriptsNMsubscriptsN""NNNN $ $ $$ $ $
•
• >eclares variables and allocates stora#e space.>eclares variables and allocates stora#e space. •
• Variables declared withVariables declared with DimDim at the script level are available to all at the script level are available to all
procedures within the script.
procedures within the script.
•
• At the procedure level, variables are available only within theAt the procedure level, variables are available only within the
procedure.
procedure.
•
• @ @ou can also ou can also use theuse the DimDim statement with empty parentheses to statement with empty parentheses to
declare a dynamic array.
declare a dynamic array.
•
• Note Note When you use theWhen you use the DimDim statement in a procedure, you statement in a procedure, you
#enerally put the
#enerally put the DimDim statement at the be#innin# of the statement at the be#innin# of the
procedure.
•
• Private Private varnameM varnameM!!MsubscriptsNMsubscriptsN""NMNM## varnameMvarnameM!!MsubscriptsNMsubscriptsN""NNNN $ $ $$ $ $
•
•
>eclares private variables and allocates stora#e space.
>eclares private variables and allocates stora#e space.
••
9rivate stateme
9rivate stateme
nt variables are available only to the
nt variables are available only to the
script in which
script in which
they are declared.
they are declared.
•
•
The followin# e
The followin# e
%ample illustrates u
%ample illustrates u
se of the
se of the
Private
Private
statement5
statement5
Private
Private "y4umber "y4umber ‘‘ 9rivate Variant variable. 9rivate Variant variable.
Private
Private "yArray&-' "yArray&-' ‘‘ 9rivate array variable. 9rivate array variable.
‘
‘ "ultiple 9rivate decl "ultiple 9rivate declarations of Variant variables.arations of Variant variables.
Private
•
•
Public
Public
varnameM
varnameM
!
!
MsubscriptsN
MsubscriptsN
"
"
NM
NM
#
#
varnameM
varnameM
!
!
MsubscriptsN
MsubscriptsN
"
"
NN
NN
$ $ $
$ $ $
••
>eclares public variables and allocates stora#e space.
>eclares public variables and allocates stora#e space.
•
•
Public
Public statement variables are available to all
statement variables are available to all procedur
procedur
es in
es in
all scripts.
all scripts.
•
•
@
@
ou can also use th
ou can also use th
e
e
Public
Public
statement with empty
statement with empty
parentheses to declare a dynamic array.
parentheses to declare a dynamic array.
Public
Public "y4umber "y4umber ‘‘ 9ublic Variant variable. 9ublic Variant variable.
Public
Public "yArray&-' "yArray&-' ‘‘ 9ublic array variable. 9ublic array variable.
‘
‘ "ultiple 9ublic decl "ultiple 9ublic declarations of Variant variables.arations of Variant variables.
Public