Skip navigation

A Workaround for Events

Downloads
7822.zip

VBScript classes don't support events. In other words, VBScript doesn't have a system-provided facility that lets classes expose events that a client application can detect and properly handle. However, you can create a class that lets client applications pass in the code to execute in correspondence to class events. The class exposes a property for each event it wants to support. The client application sets this property with a function.

For example, suppose you want the FileList class to expose the OnFileMatch property each time you add a new filename to the array. To create this pseudoevent, you need to create a script for the client application and revise the class code.

Listing A contains an excerpt from the client application's script PseudoEventClientCode. (You can find the entire PseudoEventClientCode script on the Win32 Scripting Journal Web site.) The client application uses this script to handle the event. As Listing A shows, you use the AppendToString function to set the OnFileMatch property. The AppendToString function accepts the new filename as its argument.

Listing B contains an excerpt from the script PseudoEventClassCode, a revised version of the FileList class code. (You can find the entire PseudoEventClassCode script on the Win32 Scripting Journal Web site.) As Listing B shows, you format the string that VBScript will evaluate at runtime by concatenating the OnFileMatch property with the necessary parentheses and the new filename. Because the filename is a string, you must enclose it in quotation marks, which the VBScript constant Chr(34) specifies. After you format the string, you use the Eval function to evaluate it, assign the result to the rc variable, and manipulate that result as needed.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish