Windows Script Components  

Exposing Methods

Methods are implemented as functions or subroutines in your Windows® Script Component file.

To expose a method

  1. Create a <public> element as a child of the <component> element.
  2. In the <public> element, include a <method> element. The method element can optionally include one or more <parameter> elements to define the method's parameters.
  3. Write a procedure in any scripting language to implement the function. Place the procedure in a <script> element outside the <implements> element but within the <component> element. Be sure the function name matches the functionName, or if you did not specify a functionName, the methodName name you specified in the <method> element.

    For example, the following example shows a fragment from a script component file with two methods, factorial and 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.
       <method name="factorial"/>
       <method name="random" internalName="getRandomNumber">
          <parameter name="upperBound"/>
          <parameter name="seed"/>
    <script language="VBScript">
    Function factorial(n)
       If isNumeric(n) Then
          If n <= 1 Then
             factorial = 1
             factorial = n*factorial(n-1)
          End If
          factorial = -2   ' Error code.
       End If
    End Function
    Function getRandomNumber(upperBound, seed)
       getRandomNumber = Cint(upperBound * Rnd(seed) + 1)
    End Function

You can specify a default method for a script component so the host application can invoke the method without explicitly calling it. For example, if you have exposed a method called factorial and marked it as the default, you can call it in the followoing ways in Visual Basic:

Set component = CreateObject("component.MyComponent")
n = component.factorial(4)   ' Calls factorial method explicitly.
n = component(4)   ' Calls factorial method as default.

To specify a default method, include an attribute assigning a special dispatch identifier (a dispid) to the method. For more information about dispids, see Exposing Events.

To specify a default method

See Also

Exposing Events | Exposing Properties | Script Component File Contents