Microsoft® Windows® Script Component
<method> Element
 Script Component Reference

See Also


Declares a method.

Syntax

<method name="methodName" internalName="functionName" dispid=dispID>
      
[<parameter name="parameterID"/>]
</method>

Values

methodName

The name of the method to expose.

functionName

(Optional) The name of the procedure (function or subroutine) in the script component file that implements the method. If you do not specify an internal name, methodName is used.

Tip   In XML, you can implement elements that have no content (such as the <method> element) by closing the element with />.

dispID

(Optional)   COM dispatch ID for the method. If no dispid is specified, the dispid is automatically generated. If the dispid is set to 0 (zero), the method becomes the script component's default method. For more information about dispids, see Exposing Events.

parameterID

If a parameter is explicitly declared for the method, identifies the parameter's name.

Remarks

A method is implemented as a procedure (function or subroutine) in a separate <script> element. The <method> element maps the name of the method to the procedure used to implement it.

You can optionally declare the parameters for your method. Doing so is not required, but exposes parameter information if you generate a type library for your script component (see Creating a Script Component Type Library).

Example

The following script component fragment defines two methods (factorial and random). The random method includes definitions for its parameters, and binds to a function called getRandomNumber.

Note   A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.

<public>
   <method name="factorial"/>
   <method name="random" internalName="getRandomNumber">
      <parameter name="upperBound">
      <parameter name="seed">
   </method>
</public>

<script language="VBScript">
<![CDATA[
   Function factorial(n)
      If isNumeric(n) Then
         If n <= 1 Then
             factorial = 1
         Else
             factorial = n*factorial(n-1)
         End If
      Else
         factorial = -2    ' Error code.
      End If
   End Function
   
   Function getRandomNumber(upperBound, seed)
      upperBound = CInt(upperBound)
      Randomize
      getRandomNumber = Cint(upperBound * Rnd(seed) + 1)
   End Function
]]>
</script>