Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Handling Events in a VBScript Class

Reply
 
Old 07-03-2008   #1 (permalink)
vorpal


 
 

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
Old 07-04-2008   #2 (permalink)
vorpal


 
 

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

Quote:

> vorpal wrote:
Quote:

> > 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
Old 07-05-2008   #3 (permalink)
mr_unreliable


 
 

Re: Handling Events in a VBScript Class

vorpal wrote:
Quote:

> 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
Reply

Thread Tools


Similar Threads
Thread Forum
PPT converted to HTM - handling WMP events... .NET General
Handling .NET events? PowerShell
Handling events from COM? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46