1 / 24

VBScript

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.

effief
Download Presentation

VBScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VBScript Session 7

  2. What we learn last session? • Interacting in VBScript • VBScript constants groups.

  3. Subjects for session 7 • VBScript procedures. • Sub procedures. • Function Procedures. • Getting data into and out of procedures • Call statement. • Arguments ByVal and ByRef

  4. VBScript Procedures • In VBScript, there are two kinds of procedures; • the Sub procedure and the Function procedure.

  5. 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 ().

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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

  14. 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.

  15. 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)

  16. 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)

  17. Sub Implementation • Getting an argument Function ByteMe (sngNum) ByteMe = CByte(sngNum) EndFunction • Sending an argument X = ByteMe(5.5)

  18. 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)

  19. 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.

  20. 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

  21. 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.

  22. Excercise

  23. 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.

  24. Make sure to visit us • Tutorials • Articles • Projects • And much more www.AdvancedQTP.com

More Related