Fundamentals of VB Script
Fundamentals of VB Script
www.apextgi.in
Subjects for
Session
Vbscript fundamentals.
Variant subtypes.
Variables.
Option Explicit statement.
Keywords.
Scope and liftime of variables.
Dim, Private and Public statements.
Rem statement.
2
What is VScript!
"
# Safe subset of visual basic
"
$he VScript %ode is case &nsensitive.
"
'icrosoft Visual asic Scripting (dition brings active
scripting to a wide variety of environments, including Web
client scripting in 'icrosoft &nternet (xplorer and Web
server scripting in 'icrosoft &nternet &nformation Service
)&&S*.
+
'icrosoft
,
Script $imeline
-
Internet
Explorer 3.0
8/96
Version
1.0
Version
2.0
IIS 2
12/96
Internet
Explorer 4.0
8/97
Version
3.0
Windows
98
Version
3.1
Version
4.0
Version
5.0
Visul
Studio 6
Internet
Explorer 5.0
Win !" 5.0
!ote# $ui%&"est uses V'S%ript 5.6
!ote# $ui%&"est uses V'S%ript 5.6
Scripting for Specific .latforms
"
VScript
/
designed to run on multiple platforms that use the #ctive0
Scripting &nterface.
"
V# )V for #pplications*
/
'ore sophisticated customi1ation.
/
.ower user focus, natural upgrade, superb integration
"
Visual asic
/
est tool for distributed client2Server solutions.
/
.owerful, robust, scalable.
3
"
VScript has only one data type called a Variant.
"
# Variant is a special 4ind of data type that can contain
different 4inds of information, depending on how it is used.
"
ecause Variant is the only data type in VScript, it is also
the data type returned by all functions in VScript.
"
#t its simplest, a Variant can contain either numeric or string
information.
"
# Variant behaves as a number when you use it in a
numeric context and as a string when you use it in a string
context.
5
VScript 6ata $ypes
Variant Subtypes
"
eyond the simple numeric or string classifications, a Variant
can ma4e further distinctions about the specific nature of
numeric information.
"
7or example, you can have numeric information that
represents a date or a time.
"
When used with other date or time data, the result is always
expressed as a date or a time.
"
8ou can also have a rich variety of numeric information ranging
in si1e from oolean values to huge floating9point numbers.
:
VScript 6ata $ypes
Variant Subtypes
;
Subtype Description
E(pt) Variant is uninitili*ed. Vlue is 0 +or nu(eri% ,ri-les or *ero.len/t0 strin/ 1223 +or strin/
,ri-les.
!ull Variant intentionll) %ontins no ,lid dt.
'oolen 4ontins eit0er True or False.
')te 4ontins inte/er in t0e rn/e 0 to 255.
Inte/er 4ontins inte/er in t0e rn/e .325768 to 325767.
4urren%) .9225337520356855477.5808 to 9225337520356855477.5807.
6on/ 4ontins inte/er in t0e rn/e .2514754835648 to 2514754835647.
Sin/le 4ontins sin/le.pre%ision5 +lotin/.point nu(-er in t0e rn/e .3.402823E38 to .1.401298E.45 +or
ne/ti,e ,lues7 1.401298E.45 to 3.402823E38 +or positi,e ,lues.
8ou-le 4ontins dou-le.pre%ision5 +lotin/.point nu(-er in t0e rn/e .1.79769313486232E308 to
.4.94065645841247E.324 +or ne/ti,e ,lues7 4.94065645841247E.324 to 1.79769313486232E308
+or positi,e ,lues.
8te 4ontins nu(-er t0t represents dte -etween 9nur) 15 100 to 8e%e(-er 315 9999.
Strin/ 4ontins ,ri-le.len/t0 strin/ t0t %n -e up to pproxi(tel) 2 -illion %0r%ters in len/t0.
:-;e%t 4ontins n o-;e%t.
Error 4ontins n error nu(-er.
Variables
"
# variable is a convenient placeholder that refers to a
computer memory location where you can store program
information that may change during the time your script is
running.
"
7or example, you might create a variable called %lic4%ount
to store the number of times a user clic4s an ob<ect on a
particular Web page.
"
you only have to refer to a variable by his name to see or
change its value.
=
6eclaring Variables
"
8ou declare variables explicitly in your script using the Dim
statement, the Public statement, and the Private statement. 7or
example>
Dim 6egrees7ahrenheit
"
8ou declare multiple variables by separating each variable name
with a comma. 7or example>
Dim $op, ottom, ?eft, @ight
1A
6eclaring Variables
"
8ou can also declare a variable implicitly by simply
using its name in your script.
"
$hat is not generally a good practice because you
could misspell the variable name in one or more
places, causing unexpected results when your script
is run.
"
7or that reason, the Option Explicit statement is
available to reBuire explicit declaration of all
variables.
11
Cption (xplicit Statement
"
7orces explicit declaration of all variables in a script.
"
&f used, the Option Explicit statement must appear in a
script before any other statements.
"
When you use the Option Explicit statement, you must
explicitly declare all variables using the Dim, Private, Public,
or ReDim statements. &f you attempt to use an undeclared
variable name, an error occurs.
"
Tip Dse Option Explicit to avoid incorrectly typing the
name of an existing variable or to avoid confusion in code
where the scope of the variable is not clear.
12
"
'ust begin with an alphabetic character.
"
%annot contain an embedded period.
"
'ust not exceed 233 characters.
"
'ust be uniBue in the scope in which it is declared.
1+
Tip (enin/+ull pre+ix to ,ri-les to indi%te t0e
su-t)pes i.e
i4ounter 1inte/er35 str!(e 1Strin/35 -<l/ 1'oolen35
dte"od) 18te3
Tip (enin/+ull pre+ix to ,ri-les to indi%te t0e
su-t)pes i.e
i4ounter 1inte/er35 str!(e 1Strin/35 -<l/ 1'oolen35
dte"od) 18te3
"
(mpty
/
$he Empty 4eyword is used to indicate an uninitiali1ed variable value.
"
Eull
/
$he Null 4eyword is used to indicate that a variable contains no valid data.
"
$rue
/
$he True 4eyword has a value eBual to 91.
"
7alse
/
$he False 4eyword has a value eBual to A.
"
Eothing
/
$he Nothin 4eyword in VScript is used to disassociate an ob<ect
variable from any actual ob<ect.
1-
"
# variableFs scope is determined by where you declare it.
"
When you declare a variable within a procedure, only code within that procedure can
access or change the value of that variable.
"
&f you declare a variable outside a procedure, you ma4e it recogni1able to all the
procedures in your script.
"
$he lifetime of a variable depends on how long it exists.
"
$he lifetime of a script9level variable extends from the time it is declared until the time
the script is finished running.
"
#t procedure level, a variable exists only as long as you are in the procedure.
"
ou can have local variables of the same name in several different procedures because
each is recogni1ed only by the procedure in which it is declared.
13
"
Dim varnameG!GsubscriptsH"HG# varnameG!GsubscriptsH"HH $ $ $
"
6eclares variables and allocates storage space.
"
Variables declared with Dim at the script level are available to all
procedures within the script.
"
#t the procedure level, variables are available only within the procedure.
"
8ou can also use the Dim statement with empty parentheses to declare a
dynamic array.
"
Note When you use the Dim statement in a procedure, you generally put
the Dim statement at the beginning of the procedure.
15
"
Private varnameG!GsubscriptsH"HG# varnameG!GsubscriptsH"HH $ $ $
"
6eclares private variables and allocates storage space.
"
.rivate statement variables are available only to the script in which they are
declared.
"
$he following example illustrates use of the Private statement>
1:
Private =)!u(-er I... >ri,te Vrint ,ri-le.
Private =)?rr)193 I... >ri,te rr) ,ri-le.
I... =ultiple >ri,te de%lrtions o+ Vrint ,ri-les.
Private =)!u(-er5 =)Vr5 @our!u(-er
"
Public varnameG!GsubscriptsH"HG# varnameG!GsubscriptsH"HH $ $ $
"
6eclares public variables and allocates storage space.
"
Public statement variables are available to all procedures in all scripts.
"
8ou can also use the Public statement with empty parentheses to
declare a dynamic array.
1;
Public =)!u(-er I... >u-li% Vrint ,ri-le.
Public =)?rr)193 I... >u-li% rr) ,ri-le.
I... =ultiple >u-li% de%lrtions o+ Vrint ,ri-les.
Public =)!u(-er5 =)Vr5 @our!u(-er
Public =)!u(-er I... >u-li% Vrint ,ri-le.
Public =)?rr)193 I... >u-li% rr) ,ri-le.
I... =ultiple >u-li% de%lrtions o+ Vrint ,ri-les.
Public =)!u(-er5 =)Vr5 @our!u(-er
1=
Contact Us @ :
E-20, sector !", #oi$a
0%20 &02'000(&02'02)( &02'02*
Visit us % http%&&'''$apexti$in