Windows Vista Forums

Handling Events in a VBScript Class

  1. #1


    vorpal Guest

    Handling Events in a VBScript Class

    I can handle events fired by an object as follows:

    set objTest=WScript.CreateObject("TheObject.TheFunction","objTest_")

    Sub objTest_TheEvent
    'The event handler code.
    End Sub

    The above works quite nicely.

    I want to do the following:
    Class WrapperClass

    Sub Class_Initialize
    set objTest=WScript.CreateObject("TheObject.TheFunction","objTest_")
    End Sub

    Sub objTest_TheEvent
    'The event handler code.
    End Sub

    End Class

    When I try the second way, the event handler is never called. If I
    move it outside the class, it DOES get called, surprisingly enough,
    even though the object is declared and initialized inside the class.

    Is there any way I can modify this so that the event handler is a
    class member?



    Thanks,
    --Vorpal

      My System SpecsSystem Spec

  2. #2


    vorpal Guest

    Re: Handling Events in a VBScript Class

    On Jul 4, 6:27*am, mr_unreliable <kindlyReplyToNewsgr...@xxxxxx>
    wrote:
    jw,
    Thanks for the helpful, informative reply. I had tried wsc's but was
    trying with "CreateObject". Now I know better.

    My workaround for the other problem was to have a global sub call the
    class member I really wanted to usr. This DOES work, but it breaks
    encapsulation. Breaks it? Heck, it crushes it, but like I say, it
    does work. Ugly, Ugly

    I'll try the wsc approach for rev 2.0 This has the added advantage
    that once created and working, I can just leave the class in a file
    and the scripts that use it get leaner.

    Thanks Again,
    --Vorpal


    > vorpal wrote:

    > > Is there any way I can modify this so that the event handler is a
    > > class member?
    >
    > hi Vorpal, I don't think you are going to get a constructive
    > answer to this query.
    >
    > I tried your modifying your code, and using "GetRef" to connect
    > your subroutine to an object's events. *That didn't work either.
    >
    > I suspect that the scripting engine is looking for a "global"
    > subroutine name to connect to, rather than looking for a sub
    > name in the "class namespace". *I would consider this to be a
    > "bug", but since microsoft has given up on maintaining vbs,
    > reporting bugs is futile.
    >
    > As an alternative, you might consider restructuring your class
    > code as a "windows script component" (a.k.a. a "wsc" file).
    > WSC's are similar to class code, but structured a bit differently.
    > More to the point, a wsc file DOES allow for sinking events.
    > Instead of CreateObject, you use an object tag:
    >
    > <object id="oATO" progid="wshAPIToolkit.ucATO" events="True"
    > reference="yes" />
    >

      My System SpecsSystem Spec

  3. #3


    mr_unreliable Guest

    Re: Handling Events in a VBScript Class

    vorpal wrote:

    > My workaround for the other problem was to have a global sub call the
    > class member I really wanted to usr. This DOES work, but it breaks
    > encapsulation. Breaks it? Heck, it crushes it, but like I say, it
    > does work. Ugly, Ugly
    >
    Yes Vorpal, the life of a scripter is filled with disappointments.
    vbs is somewhat limited in what it can do, and so you-don't-always-
    get-what-you-want. Scripters must frequently resort to:
    work-arounds, compromises, brute-force -- and yes --
    just plain UGLY code...

    Your only consolation is that your boss doesn't usually care
    about ugly, as long as you meet your project completion target
    date...

    cheers, jw

      My System SpecsSystem Spec

Handling Events in a VBScript Class

Similar Threads
Thread Thread Starter Forum Replies Last Post
PPT converted to HTM - handling WMP events... emailceloftis .NET General 0 16 Jul 2009
Handling .NET events? Duncan Smith PowerShell 11 01 Aug 2007
Handling events from COM? Duncan Smith PowerShell 1 18 Jul 2007
Handling events in control templates Illumineo Avalon 1 07 Aug 2006
Handling Drag Events in Custom Control viRtual Avalon 2 04 Jun 2006