VBSCRIPT VARIABLES AND DATATYPES VBSCRIPT VARIABLES AND DATATYPES
What Is a Variable? What Is a Variable?
A variable is a convenient placeholder that refers to a computer memory location A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change
where you can store program information that may change during executionduring execution
Declaring Variables
Declaring Variables
We declare variables explicitly in our script using
We declare variables explicitly in our script using thethe DimDim statement, the statement, the PublicPublic
statement, and the
statement, and the PrivatePrivate statement. We declare multiple variables by separating statement. We declare multiple variables by separating each variable name with a comma.
each variable name with a comma. For example:
For example: Dim TDim Topop, ottom, , ottom, !eft, "ight!eft, "ight
We can also declare a variable implicitly by simply using its name in our script. We can also declare a variable implicitly by simply using its name in our script. ecause we may misspell the variable name in one or more places, causing ecause we may misspell the variable name in one or more places, causing unexpected results During execution. For that reason,
unexpected results During execution. For that reason, thethe Option ExplicitOption Explicit statement statement is available to re#uire explicit declaration of
is available to re#uire explicit declaration of all variables. The $ption %xplicitall variables. The $ption %xplicit statement should be the first statement in the script.
statement should be the first statement in the script.
Option explicit: Option explicit:
When We use the $ption %xplicit statement, you must explicitly declare all variables When We use the $ption %xplicit statement, you must explicitly declare all variables using the Dim, &rivate, &ublic, or "eDim statements. 'f
using the Dim, &rivate, &ublic, or "eDim statements. 'f we attempt to use anwe attempt to use an undeclared variable name, causing an error occurs.
undeclared variable name, causing an error occurs. (aming "estrictions
(aming "estrictions )aria
)ariable names follow the ble names follow the standard rules for naming anything in )*cript. Astandard rules for naming anything in )*cript. A variable name:
variable name:
•
• +ust begin with an alphabetic character.+ust begin with an alphabetic character. •
• annot contain an eannot contain an embedded period.mbedded period. •
• +ust not exceed - characters.+ust not exceed - characters. •
• +ust be uni#ue in the scope in which it is declared.+ust be uni#ue in the scope in which it is declared.
Assigning )alues to )ariables Assigning )alues to )ariables
)alues are assigned to variables creating an expression as
)alues are assigned to variables creating an expression as follows: the variable isfollows: the variable is on the left
on the left side of the expression and the value you want to assign to the variable isside of the expression and the value you want to assign to the variable is on the right.
on the right. For
DATATYPES
What Are VBScript Data Types?
VBScript has nly ne !ata type calle! a Variant" A Variant is a special #in! $ !ata type that can cntain !i$$erent #in!s $ in$r%atin& !epen!ing n h' it(s )se!. ecause )ariant is the only data type in )*cript, it1s also the data type returned by all functions in )*cript.
At its simplest, a )ariant can contain either numeric or string information. A )ariant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you1re wor2ing with data that loo2s li2e numbers, )*cript assumes that it is numbers and does the thing that is most appropriate for numbers. *imilarly, if you1re wor2ing with data that can only be string data, )*cript treats it as string data. $f course, you can always ma2e numbers behave as strings by enclosing them in #uotation mar2s 34 45.
Variant S)btypes
The $ll'ing table sh's the s)btypes $ !ata that a Variant can cntain"
*ubtype Description
E%pty Variant is )ninitiali*e!" Val)e is + $r n)%eric ,ariables r a *er-length string .//0 $r string ,ariables"
N)ll Variant intentinally cntains n ,ali! !ata" Blean Cntains either Tr)e r 1alse"
Byte Cntains integer in the range + t 233"
Integer Cntains integer in the range -42&567 t 42&565" C)rrenc
y
-822&445&2+4&673&955"37+7 t 822&445&2+4&673&955"37+5"
Lng Cntains integer in the range -2&:95&974&697 t 2&:95&974&695"
Single Cntains a single-precisin& $lating-pint n)%ber in the range -4"9+2724E47 t -:"9+:287E-93 $r
psiti,e ,al)es"
D)ble Cntains a !)ble-precisin& $lating-pint n)%ber in the range -:"585684:4976242E4+7 t
-9"89+6369379:295E-429 $r negati,e ,al)es;
9"89+6369379:295E-429 t :"585684:4976242E4+7 $r psiti,e ,al)es"
Date .Ti%e0
Cntains a n)%ber that represents a !ate bet'een <an)ary :& :++ t Dece%ber 4:& 8888"
String Cntains a ,ariable-length string that can be )p t appr=i%ately 2 billin characters in length"
>bect Cntains an bect"
Errr Cntains an errr n)%ber"
Operator Precedence
When se,eral peratins cc)r in an e=pressin& each part is e,al)ate! an! resl,e! in a pre!eter%ine! r!er calle! peratr prece!ence . 6ou can use parentheses to override the order of precedence and force some parts of an
expression to be evaluated before others. $perations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained.
When e=pressins cntain peratrs $r% %re than ne categry& arith%etic peratrs are e,al)ate! $irst& c%parisn peratrs are e,al)ate! ne=t& an! lgical peratrs are e,al)ate! last" C%parisn
peratrs all ha,e e@)al prece!ence; that is& they are e,al)ate! in the le$t-t-right r!er in 'hich they appear" Arith%etic an! lgical peratrs ar e e,al)ate! in the $ll'ing r!er $ prece!ence"
Arithmetic
Description SymbolExponentiation
^
Unary negation
-Multiplication
*
Division
/
Integer division
\
Modulus arithmetic
Mod
Addition
+
Subtraction
-String concatenation
&
omparison
Description SymbolE!uality
"
Ine!uality
#$
%ess than
#
reater than
$
%ess than or e!ual to
#"
reater than or e!ual to
$"
'b(ect e!uivalence
Is
%ogical
Description Symbol
Logical negation
Not
Logical conjunction
And
Logical disjunction
Or
Logical exclusion
Xor
Logical equivalence
Eqv
Logical imlication
!m
When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. !i2ewise, when addition and subtraction
occur together in an expression, each operation is evaluated in order of appearance from left to right.
The string concatenation 375 operator is not an arithmetic operator, but in precedence it falls after all arithmetic operators and before all comparison
operators. The 's operator is an ob8ect reference comparison operator. 't does not compare ob8ects or their values9 it chec2s only to determine if two ob8ect
references refer to the same ob8ect.
The lgic $ >R is that i$ at least ne $ the inp)ts are Tr)e& the )tp)t is tr)e;
Tr)e >R Tr)e Tr)e Tr)e >R 1alse Tr)e 1alse >R Tr)e Tr)e 1alse >R 1alse 1alse
>R nly ret)rns Tr)e i$ ne an! nly ne $ the inp)ts is Tr)e" Tr)e r Tr)e 1alse
Tr)e r 1alse Tr)e 1alse r Tr)e Tr)e 1alse r 1alse 1alse
I%p ret)rns $alse i$ $irst ne is tr)e an! secn! ne $alse e=cept this it ret)rns tr)e"
Tr)e i%p Tr)e Tr)e Tr)e i%p 1alse 1alse 1alse i%p Tr)e Tr)e 1alse i%p 1alse Tr)e