Need Help with VBScript? Try Vbsedit!

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.


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

     CopyCode imageCopy Code
       <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 following ways in Visual Basic:

 CopyCode imageCopy Code
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

  • In the <method> element, include the attribute dispid="0", as shown in the following example:

     CopyCode imageCopy Code
       <method name="factorial" dispid="0"/>

    This technique can be used to assign either a default method or a default property, but not both. There can be only one element in the script component with the dispid of zero.

See Also

Need Help with VBScript? Download VbsEdit!

This package includes VbsEdit 32-bit, VbsEdit 64-bit, HtaEdit 32-bit and HtaEdit 64-bit.
The evaluation version never expires.


Copyright © 2001-2018 Adersoft