• No results found

4 Script Procedres

In document VB Script (Page 27-37)

In VBScript< t+ere are t6o >inds o procedures a7ailableE t+e S procedure and t+e Fnction procedure5

##.# S Procedres

 Sub procedure is a series o VBScript statements (enclosed by Sub and 'nd Sub statements) t+at perorm actions but don9t return a 7alue5

 Sub procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed by a callin, procedure)5

I a Sub procedure +as no ar,uments< its Sub statement must include an empty set o parent+eses ()5

SyntaF

Sub -rocedure name () Statements

...

...

'nd Sub Or

Sub -rocedure name (ar,ument1< ar,ument2) Statements

...

...

'nd Sub ,0ample: #

Sub Con7ertTemp()

temp G InputBo(-lease enter t+e temperature in de,rees /5< 1) 8s,Bo T+e temperature is  P Celsius(temp) P  de,rees C5

'nd Sub ,0ample: (

##.( Fnction Procedres

 /unction procedure is a series o VBScript statements enclosed by t+e /unction and 'nd /unction statements5

 /unction procedure is similar to a Sub procedure< but can also return a 7alue5

 /unction procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed to it by a callin, procedure)5

 /unction returns a 7alue by assi,nin, a 7alue to its name in one or more statements o t+e procedure5 T+e return type o a /unction is al6ays a Variant5 SyntaF

/unction -rocedure name () Statements

...

...

'nd /unction Or

/unction -rocedure name (ar,ument1< ar,ument2) Statements

...

...

'nd /unction ,0ample: #

/unction Celsius(De,rees)

Celsius G (De,rees . 32) N  " % 'nd /unction

,0ample: (

/unction cal(a<b<c) cal G (aAbAc) 'nd /unction

##.+ Jetting Data into and ot of Procedres

o 'ac+ piece o data is passed into our procedures usin, an ar,ument5

o r,uments ser7e as place+olders or t+e data 6e 6ant to pass into our procedure5 :e can name our ar,uments any 7alid 7ariable name5

o :+en 6e create a procedure usin, eit+er t+e Sub statement or t+e /unction statement< parent+eses must be included ater t+e name o t+e procedure5

o ny ar,uments are placed inside t+ese parent+eses< separated by commas5

##.- 2sing S and Fnction Procedres in Code

 /unction in our code must al6ays be used on t+e ri,+t side o a 7ariable assi,nment or in an epression5

/or eampleF

Temp G Celsius(De,rees) .Or.

8s,Bo T+e Celsius temperature is  P Celsius(De,rees) P  de,rees5

To call a Sub procedure rom anot+er procedure< type t+e name o t+e procedure alon, 6it+ 7alues or any reuired ar,uments< eac+ separated by a comma5 T+e Call statement is not reuired< but i you do use it< you must enclose any ar,uments in parent+eses5

T+e ollo6in, eample s+o6s t6o calls to t+e 8y-roc procedure5 One uses t+e Call statement in t+e codeE t+e ot+er doesn9t5 Bot+ do eactly t+e same t+in,5

Call 8y-roc(irstar,< secondar,) 8y-roc irstar,< secondar,

@otice t+at t+e parent+eses are omitted in t+e call 6+en t+e Call statement isn9t used5

!4 Script 4ilt in Fnctions

Types of Fnctions

o Con7ersions (2)

o Dates"Times (1%)

o /ormattin, Strin,s (4)

o Input"Output (3)

o 8at+ (%)

o 8iscellaneous (3)

o 0oundin, ()

o Strin,s (3)

o Variants ($)

Important Fnctions

#' As Fnction

0eturns t+e absolute 7alue o a number5 Dim num

numGabs(.533) ms,bo num

(' Array Fnction

0eturns a 7ariant containin, an rray Dim 

Grray(+yderabad<c+ennai<mumbai) ms,bo ()

0eDim ()

(4)Gnellore

ms,bo (4) +' Asc Fnction

0eturns t+e @SI c+aracter code correspondin, to t+e irst letter in a strin,5 Dim num

numGsc() ms,bo num

N It returns t+e 7alue ! N -' C)r Fnction

0eturns t+e c+aracter associated 6it+ t+e speciied @SI c+aracter code5 Dim c+ar

C+arGC+r(!) ms,bo c+ar N It returns  N 7' CInt Fnction

0eturns an epression t+at +as been con7erted to a !ariant o subtype Integer5 Dim num

numG12354

myIntGCInt(num) ms,bo 8yInt 9' Date Fnction

0eturns t+e Current System Date5 Dim mydate

mydateGDate ms,bo mydate

' Day Fnction '1) Dim myday

mydayGDay(1#<December<2%) ms,bo myday

'2) Dim myday mydateGdate

mydayGDay(8ydate) ms,bo myday

;' DateDiff Fnction

0eturns t+e number o inter7als bet6een t6o dates5 Dim myday

mydateGK/(@#@(//K

0GDatediffLdL?mydate?*ow' msgo0 0

' %or Fnction

0eturns a 6+ole number bet6een  and 23< inclusi7e< representin, t+e +our o t+e day5

Dim mytime< 8y+our mytimeG@o6

my+ourG+our (mytime) ms,bo my+our

#/' Moin Fnction

0eturns a strin, created by ?oinin, a number o substrin,s contained in an array5 Dim mystrin,< myarray(3)

myarray()GC+andra  myarray(1)G8o+an  myarray(2)G0eddy

##' ,val Fnction

'7aluates an epression and returns t+e result5

#(' Time Fnction

0eturns a !ariant o subtype Date indicatin, t+e current system time5 Dim mytime

mytimeGTime ms,bo mytime

#+' !arType Fnction

0eturns a 7alue indicatin, t+e subtype o a 7ariable5 Dim 8yC+ec>

8yC+ec> G VarType(3) 9 0eturns 25 8s,bo 8yc+ec>

8yC+ec> G VarType(1"1%"!2) 9 0eturns #5 8s,bo 8yc+ec>

8yC+ec> G VarType(VBScript) 9 0eturns $5 8s,bo 8yc+ec>

#-' 8eft Fnction

Dim 8yStrin,< *etStrin, 8yStrin, G VBSCript

*etStrin, G *et(8yStrin,< 3) 9 *etStrin, contains VBS5

#-' 6ig)t Fnction Dim nyStrin,< 8yStr

nyStrin, G ;ello :orld 9 Deine strin,5 8yStr G 0i,+t(nyStrin,< 1) 9 0eturns d5 8yStr G 0i,+t(nyStrin,< !) 9 0eturns  :orld5

8yStr G 0i,+t(nyStrin,< 2) 9 0eturns ;ello :orld5

#7' 8en Fnction

0eturns t+e number o c+aracters in a strin, or t+e number o bytes reuired to store a 7ariable5

' 1)F Dim 8ystrin,

mystrin,G*en(&5C50eddy) ms,bo mystrin,

' 2)F Dim 8ystrin,

8ystrin,GInputbo('nter a Value) 8ystrin,G*en(8ystrin,)

8s,bo 8ystrin,

#9' "id Fnction

0eturns a speciied number o c+aracters rom a strin,5 Dim 8yVar

8yVar G 8id(VB Script is un< 4< !) 8s,bo 8yVar

N It 0eturns XScriptY N

#' Timer Fnction

0eturns t+e number o seconds t+at +a7e elapsed since 12F 8 (midni,+t)5 /unction myTime(@)

Dim StartTime< 'ndTime StartTime G Timer

/or I G 1 To @   @et

'ndTime G Timer

myTimeG 'ndTime . StartTime ms,bo myTime

'nd /unction

Call myTime(2)

#' is*meric Fnction Dim 8yVar< 8yC+ec>

8yVar G 3

8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>

8yVar G 4%5%

8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>

8yVar G 4 ;elp

8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>

N It 0eturns True"/alse li>e 0esult N

#;' Inpto0 Fnction

Displays a prompt in a dialo, bo< 6aits or t+e user to input tet or clic> a button<

and returns t+e contents o t+e tet bo5 Dim Input

Input G InputBo('nter your name) 8s,Bo (Lou enteredF  P Input)

Dim 8yVar

8yVar G 8s,Bo (;ello :orld< !< 8s,Bo 'ample)

!4Script synta0 rles and gidelines

(#.# Case@sensitivity:

By deault< VBScript is not case sensiti7e and does not dierentiate bet6een upper case and lo6er.case spellin, o 6ords< or eample< in 7ariables< ob?ect and met+od names< or constants5

/or eample< t+e t6o statements belo6 are identical in VBScriptF

Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:eb*ist(toDay)5Select 31

bro6ser(mercury)5pa,e(ind a li,+tF)56eblist(today)5select 31

(#.( Te0t strings:

:+en 6e enter a 7alue as a tet strin,< 6e must add uotation mar>s beore and ater t+e strin,5 /or eample< in t+e abo7e se,ment o script< t+e names o t+e :eb site< :eb pa,e< and edit bo are all tet strin,s surrounded by uotation mar>s5

@ote t+at t+e 7alue 31 is also surrounded by uotation mar>s< because it is a tet strin, t+at represents a number and not a numeric 7alue5

In t+e ollo6in, eample< only t+e property name (irst ar,ument) is a tet strin, and is in uotation mar>s5 T+e second ar,ument (t+e 7alue o t+e property) is a 7ariable and t+ereore does not +a7e uotation mar>s5 T+e t+ird ar,ument (speciyin, t+e timeout) is a numeric 7alue< 6+ic+ also does not need uotation mar>s5

Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:ait-roperty(items count< TotalZItems<

2)

(#.+ !ariales:

:e can speciy 7ariables to store strin,s< inte,ers< arrays and ob?ects5 Hsin, 7ariables +elps to ma>e our script more readable and leible

(#.- Parent)eses:

To ac+ie7e t+e desired result and to a7oid errors< it is important t+at 6e use parent+eses () correctly in our statements5

(#.7 Indentation:

:e can indent or outdent our script to relect t+e lo,ical structure and nestin, o t+e statements5

(#.9 Comments:

:e can add comments to our statements usin, an apostrop+e (9)< eit+er at t+e be,innin, o a separate line< or at t+e end o a statement5 It is recommended t+at 6e add comments 6+ere7er possible< to ma>e our scripts easier to understand and maintain5

(#. Spaces:

:e can add etra blan> spaces to our script to impro7e clarity5 T+ese spaces are i,nored by VBScript5

,rrors

:e +a7e t6o types 'rrors in VB ScriptE t+ey are VBScript 0un.time 'rrors and VBScript Synta 'rrors

#+.# !4Script 6n@time ,rrors

VBScript run.time errors are errors t+at result 6+en our VBScript script attempts to perorm an action t+at t+e system cannot eecute5 VBScript run.time errors occur 6+ile our script is bein, eecutedE 6+en 7ariable epressions are bein, e7aluated<

and memory is bein, dynamic allocated5

#+.( !4Script Synta0 ,rrors

VBScript synta errors are errors t+at result 6+en t+e structure o one o our VBScript statements 7iolates one or more o t+e ,rammatical rules o t+e VBScript scriptin, lan,ua,e5 VBScript synta errors occur durin, t+e pro,ram compilation sta,e< beore t+e pro,ram +as be,un to be eecuted5

In document VB Script (Page 27-37)

Related documents