ÔÚVBSÖУ¬³ÌÐò·ÖΪÁ½ÖÖ£ºSub³ÌÐòºÍFunction³ÌÐò

ÉÏһƪ / ÏÂһƪ  2008-09-10 15:30:03 / ¸öÈË·ÖÀࣺÔÚVBSÖУ¬³ÌÐò·ÖΪÁ½ÖÖ£ºSub³ÌÐòºÍFunction³ÌÐò

¶ÔÓÚSub³ÌÐòºÍFunction³ÌÐò

ÔÚ°²×°QuickTest Professionalºó¿ÉÒԲ鿴ËüµÄÎĵµ£¬QuickTest Professional HelpÀïÃæÒ²ÓйØÓÚVBscr¨©pt ProceduresµÄ˵Ã÷£º

VBscr¨©pt Procedures

In VBscr¨©pt, there are two kinds of procedures; theSubprocedure and theFunctionprocedure.

Sub Procedures

A Sub procedure is a series of VBscr¨©pt 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 ().

The following Sub procedure uses two intrinsic, or built-in, VBscr¨©pt functions,MsgBoxandInputBox, to prompt a user for information. It then displays the results of a calculation based on that information. The calculation is performed in a Function procedure created using VBscr¨©pt. The Function procedure is shown after the following discussion.

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

Function Procedures

A Function procedure is a series of VBscr¨©pt statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but can also return a value. A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a Function procedure has no arguments, its 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.

In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box.

Sub ConvertTemp()
   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

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. For example, in the following example,fDegreesis a placeholder for the value being passed into the Celsius function for conversion.

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

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.

Using Sub and Function Procedures in Code

A Function in your code must always be used on the right side of a variable assignment or in an expression. For example:

Temp = Celsius(fDegrees)

-or-

MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."

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. TheCallstatement is not required, but if you do use it, you must enclose any arguments in parentheses.

The following example shows two calls to theMyProcprocedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.

Call MyProc(firstarg, secondarg)
MyProc firstarg, secondarg

Notice that the parentheses are omitted in the call when the Call statement isn't used.

 

 

 

ÏÂÃæÊǹØÓÚ Function Definition

 

Defining the Function Definition

After you open the Function Definition Generator, you can begin defining a function.



For example, if you want to define a function that verifies the value of a specified property, you might name itVerifyPropertyand define it as apublicfunction so that it can be called from any associated component (as long as the function library is associated with its application area). (If you define it as private, the function can only be called from elsewhere in the same function library. Private functions cannot be registered to a test object.)

To define a function:

  1. In the Name box, enter a name for the new function. The name should clearly indicate what the operation does so that it can be easily selected from the Step Generator (for function libraries) or the Keyword View. Function names cannot contain non-English letters or characters. In addition, function names must begin with a letter and cannot contain spaces or any of the following characters:  ! @ # $ % ^ & * ( ) + = [ ] \ { } | ; ` : "" , / < > ?
  2. Note: Do not give the user-defined function the same name as a built-in function (for example, GetLastError, MsgBox, or Print). Built-in functions take priority over user-defined functions, so if you call a user-defined function that has the same name as a built-in function, the built-in function is called instead. For a list of built-in functions, refer to the Built-in functions list in the Step Generator (Insert > Step Generator).

  3. From the Type list, choose Function orSub, according to whether you want to define a function or a subroutine.
  4. From the Scope list, choose the scope of the function¡ªeitherPublic(to enable the function to be called by any component whose application area is associated with this function library), or Private (to enable the function to be called only from elsewhere in the same function library). By default, the scope is set toPublic. (Onlypublicfunctions can be registered to a test object.)
  5. Note: If you create a user-defined function manually and do not define the scope asPublicor Private, it will be treated as apublicfunction, by default.

    After you define apublicfunction, you can register the function, as described inRegistering a Function Using the Function Generator.Alternatively, if you defined a private function, or if you do not want to register the function, you can continue by specifying arguments for the function. For more information, seeSpecifying Arguments for the Function.


TAG:

 

ÆÀ·Ö£º0

ÎÒÀ´ËµÁ½¾ä

Open Toolbar