24
Dani Vainstein 1 VBScript Session 7

VBScript

  • Upload
    liz

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: VBScript

Dani Vainstein 1

VBScript

Session 7

Page 2: VBScript

Dani Vainstein 2

What we learn last session?

Interacting in VBScript

VBScript constants groups.

Page 3: VBScript

Dani Vainstein 3

Subjects for session 7

VBScript procedures.

Sub procedures.

Function Procedures.

Getting data into and out of procedures

Call statement.

Arguments ByVal and ByRef

Page 4: VBScript

Dani Vainstein 4

VBScript Procedures

In VBScript, there are two kinds of procedures;

the Sub procedure and the Function procedure.

Page 5: VBScript

Dani Vainstein 5

VBScript Procedures Sub Procedures

A Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub 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 ().

Page 6: VBScript

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

Page 7: VBScript

Dani Vainstein 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 Function statement causes an immediate exit from a Sub or Function procedure.

Any number of Exit Sub or Exit Function statements can appear anywhere in a Sub or Function procedure.

Page 8: VBScript

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

Caution   Sub procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.

Page 9: VBScript

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

Page 10: VBScript

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

Page 11: VBScript

Dani Vainstein 11

VBScript Procedures Function Procedures

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

Page 12: VBScript

Dani Vainstein 12

Function Procedures

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

Page 13: VBScript

Dani Vainstein 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

Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function

Sub ConvertTemp() Dim Temp temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub

Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function

Page 14: VBScript

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

Page 15: VBScript

Dani Vainstein 15

Sub Implementation

Getting an argument

Sub doLoops (iNum) Dim i For i = 1 To iNum Step 1 ‘ do nothing Next

End Sub

Sending an argument

doLoops 5 Call doLoops(5)

Page 16: VBScript

Dani Vainstein 16

Sub Implementation

Getting several arguments

Sub doLoops (iNum,strString) Dim i For i = 1 To iNum Step 1 ‘ do nothing Next

End Sub

Sending an argument

doLoops 5,”Hello” Call doLoops(5,Hello)

Page 17: VBScript

Dani Vainstein 17

Sub Implementation

Getting an argument

Function ByteMe (sngNum)

ByteMe = CByte(sngNum)

End Function

Sending an argument

X = ByteMe(5.5)

Page 18: VBScript

Dani Vainstein 18

Sub Implementation

Getting several arguments

Function ByteMe (sngNum,ByRef errNumber)

ByteMe = CByte(sngNum)

errNumber = Err.Number

End Function

Sending an argument

X = ByteMe(5.5,errNumber)

Page 19: VBScript

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

Page 20: VBScript

Dani Vainstein 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

Call MyFunction("Hello World") Function MyFunction(text)

MsgBox text End Function

Page 21: VBScript

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

Page 22: VBScript

Dani Vainstein 22

Excercise

Page 23: VBScript

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

Page 24: VBScript

Make sure to visit us

Tutorials

Articles

Projects

And much more

www.AdvancedQTP.com

24