Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Scriptom lets you subscribe to COM events on an ActiveXObject using the .events metaproperty. The event handler is a Closure.  The arguments in the Closure are passed through an instance of ActiveXObject.EventSupport.EventArguments.  Don't worry ... it works like an array. (smile)

In this example, we're subscribing to the Change event of an Excel Worksheet.  The Change event passes one argument - an Excel Range.  In the context of this code, we're assuming that the Range object contains just one cell.  In the general case, that would be a bad assumption.  Once the event handler is defined this way, every change to a cell in the worksheet is going to print a line to standard out with the column, row, and new value.

Byref Arguments

As with other COM method types, events support passing values by reference.  This is particularly important in light of the fact that events don't return a value.  If you want to be able to send data back to whoever raised the event, you have to pass values back to the sender using byref arguments.  Fortunately, this is easy to do using the EventArguments instance.

Take, for example, the following class written in Visual Basic 2005.  When you call RaisePassBooleanByref, it will raise the OnPassBooleanByref event, by reference.   This gives us an opportunity to change the value of the boolean in the event handler!

In the following Groovy code, note how the return value changes after we define the event handler, which simply inverts the value of the first argument.

Note that you may only change a value if it has been passed by reference.  Otherwise you get an exception.

  • No labels