250 likes | 270 Views
VBScript. Session 7. What we learn last session?. Interacting in VBScript VBScript constants groups. Subjects for session 7. VBScript procedures. Sub procedures. Function Procedures. Getting data into and out of procedures Call statement. Arguments ByVal and ByRef. VBScript Procedures.
E N D
VBScript Session 7
What we learn last session? • Interacting in VBScript • VBScript constants groups.
Subjects for session 7 • VBScript procedures. • Sub procedures. • Function Procedures. • Getting data into and out of procedures • Call statement. • Arguments ByVal and ByRef
VBScript Procedures • In VBScript, there are two kinds of procedures; • the Sub procedure and the Function procedure.
VBScript Procedures Sub Procedures • A Sub procedure is a series of VBScript statements (enclosed by Sub and EndSub statements) that perform actions but don't return a value. • A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). • If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().
VBScript Procedures Function Procedures • A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. • A Function procedure is similar to a Sub procedure, but can also return a value. • If a Function procedure has no arguments, the Function statement must include an empty set of parentheses. • A Function returns a value by assigning a value to its name in one or more statements of the procedure. • The return type of a Function is always a Variant. • The MsgBox and InputBox, are VBScript functions.
VBScript Procedures Sub and Function Procedures • If not explicitly specified using either Public or Private, Sub procedures are public by default, that is, they are visible to all other procedures in your script. • The value of local variables in a Sub or a Function procedure is not preserved between calls to the procedure. • You can't define a Sub procedure inside any other procedure. • The Exit Sub, Exit Functionstatement causes an immediate exit from a Sub or Function procedure. • Any number of ExitSub or ExitFunction statements can appear anywhere in a Sub or Function procedure.
VBScript Procedures Sub Procedures • Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. • However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression • You call a Sub procedure using the procedure name followed by the argument list. • CautionSub procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.
VBScript Procedures Function Procedures • To return a value from a function, assign the value to the function name. • If no value is assigned to name, the procedure returns a default value • a numeric function returns 0 • string function returns a zero-length string (""). • A function that returns an object reference returns Nothing if no object reference is assigned to name.
VBScript Procedures Function Procedures • Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. • Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. • Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.
VBScript Procedures Function Procedures • CautionA procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you have defined at the script level has the same name. • If your procedure refers to an undeclared variable that has the same name as another procedure, constant, or variable, it is assumed that your procedure is referring to that script-level name. • To avoid this kind of conflict, use an Option Explicit statement to force explicit declaration of variables.
Function Procedures • CautionVBScript may rearrange arithmetic expressions to increase internal efficiency. • Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.
Function Procedures • In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. • Sub ConvertTemp() • Dim Temp • temp = InputBox("Please enter the temperature in degrees F.", 1) • MsgBox "The temperature is " & Celsius(temp) & " degrees C." • End Sub • FunctionCelsius(fDegrees) • Celsius = (fDegrees - 32) * 5 / 9 • End Function
Getting Data into and out of Procedures • Each piece of data is passed into your procedures using an argument . • Arguments serve as placeholders for the data you want to pass into your procedure. • You can name your arguments any valid variable name. • When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. • Any arguments are placed inside these parentheses, separated by commas. • To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't.
Sub Implementation • Getting an argument Sub doLoops (iNum)Dim iFor i = 1 To iNum Step 1‘ do nothingNextEndSub • Sending an argument doLoops 5 Call doLoops(5)
Sub Implementation • Getting several arguments Sub doLoops (iNum,strString)Dim iFor i = 1 To iNum Step 1‘ do nothingNextEndSub • Sending an argument doLoops 5,”Hello” Call doLoops(5,Hello)
Sub Implementation • Getting an argument Function ByteMe (sngNum) ByteMe = CByte(sngNum) EndFunction • Sending an argument X = ByteMe(5.5)
Sub Implementation • Getting several arguments Function ByteMe (sngNum,ByRef errNumber) ByteMe = CByte(sngNum) errNumber = Err.Number EndFunction • Sending an argument X = ByteMe(5.5,errNumber)
Call Statement • To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. • The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. • The Call statement Transfers control to a Sub or Function procedure. • If you omit the Call keyword, you also must omit the parentheses around argumentlist.
Call Statement • If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded. • Call MyFunction("Hello World") • Function MyFunction(text) • MsgBox text • End Function
Arguments ByVal and ByRef • ByVal| ByRef] varname • ByVal • Indicates that the argument is passed by value. • ByRef • Indicates that the argument is passed by reference.
Lab 7.1 • Use the InputBox for retrieve two numbers,representing the flanks of a rectangle. • Write 2 functions : • First function calculates the area of the rectangle • Second function calculates the perimeter of the rectangle • The results must be displayed in a two lined msg box, each line for each result + information icon + EX 7.1 title.
Make sure to visit us • Tutorials • Articles • Projects • And much more www.AdvancedQTP.com