![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 > 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| PPT converted to HTM - handling WMP events... | .NET General | |||
| Handling .NET events? | PowerShell | |||
| Handling events from COM? | PowerShell | |||