Code, Class, and Object Modules

If you create very large scripts, or frequently reuse the same subroutines in several different scripts, you may want to split your script code into more than one file. Each script file is called a module.

A script an call subroutines and functions defined in other modules. In addition to procedures, global variables, type definitions, and enumeration definitions may be shared among modules. Just as procedures make long scripts easier to manage and debug, modules make large script projects easier to manage.

Module Types

The File | New Module command in Scripter adds new code sheets to the workspace. Each sheet is stored in a separate file. When routines in one code sheet are used by other sheets, the code sheets are called modules. Scripter supports three types of modules:

  • Code modules are used for stand-alone scripts and for storing libraries of useful procedures that can be called from other modules. The scripts described in this chapter are code modules, which contain a Main subroutine. Code modules without a Main subroutine cannot be run, but the routines contained in them can be shared by other scripts. Code modules are stored in files with a [.BAS] extension.

  • Class modules are used to define objects that you can use in other modules. A class module defines the properties and methods that the object supports. Other modules access the object's properties and methods using the same syntax that is used to access Surfer automation objects. Unlike Surfer objects, new instances of the object defined in a class module are created using the NEW keyword. Class modules are stored in files with a [.CLS] extension.

  • Object modules are identical to class modules, except that when a script uses the object defined in an object module, one instance of the object is automatically created. Additional instances of an object defined in an object module can be created with the NEW keyword. Object modules are stored in files with an [.OBM] extension.

The '#Uses Line

Before using the procedures and objects defined in another module, a script must indicate the name of the file containing the procedure or object definitions. You must place a '#Uses statement at the beginning of a script, before any procedure definitions, to instruct Scripter to load all modules used by the script. For example:

'#Uses "c:\utils.bas"

'#Uses "test.cls"

Sub Main

' use the procedures and object defined in UTILS.BAS and TEST.CLS

End Sub

Scripter does not permit cyclic '#Uses statements. That is, if module A uses module B, module B cannot use procedures from module A.

Private and Public Definitions

By default, all subroutines, functions, and user-defined types (including enumeration definitions) may be accessed from other modules. To prevent other modules from accessing procedures or user-defined types precede the definition with the Private keyword:

Private Sub MyBeep

Beep : Beep

End Sub

In contrast to procedures, the global variables defined at the top of one module are not available to other modules unless they are declared using a Public statement. When used for variable declarations, the Public statement has the same syntax as the Dim statement:

Public universal_data As String

The names of all definitions, even private ones, are visible in other modules. To avoid errors due to name conflicts you must avoid using the same procedure, type, and variable names in more than one module. A common technique for avoiding name conflicts is to append a prefix to the names of global variables and procedures. For example, if you write a module of text-processing functions, you might prefix each function name with txt (e.g., txtFunction1).

Module Properties

To set the name by which other modules refer to an object defined in a class or object module, select the Edit | Properties command. The Edit Class Module Properties dialog appears. Type the name that you want other scripts to use when referring to the object defined in the module. The instancing options control how other applications access the object defined in the module, but these options are not relevant to scripts executed within Scripter. Code modules do not have module properties.

When an object module is used in a script, one instance of the object defined in the module is automatically created. The name of the object that is automatically created is the name specified in the Edit Object Module Properties dialog.

When working with an object module or class module, open the Edit Object Module Properties or Edit Class Module Properties dialog with Edit | Properties .

Defining Object Properties and Methods

Class and object modules define the properties and methods of objects. To define the methods for an object, simply define public subroutines and functions. All the public procedures in a class or object module are methods that users of the object can call.

The properties of an object typically correspond to private global variables defined in the module. To allow users of the object to access the variable values, you provide "property get" and "property set" procedures. Use the Property Get statement to define a function that returns the value of a property. Use the Property Let statement (or the Property Set statement if the property is an object reference) to define a subroutine that changes the value of a property.

Two special subroutines are called when an object is first created and just before it is finally destroyed. In a class module, these subroutines are called "Class_Initialize" and "Class_Terminate." In an object module, these subroutines are called "Object_Initialize" and "Object_Terminate." These subroutines do not take any arguments.

Example: Defining an Object in a Class Module

The following class module demonstrates how to define an object. The sample &<<![endif]-->defines a property named Radius and a method named Draw.

' Declare a private global variable for storing the property called "Radius"

Dim cirRadius As Double

 

' Define the initialization subroutine

Private Sub Class_Initialize

cirRadius = 99

End Sub

 

' Define the termination subroutine

Private Sub Class_Terminate

End Sub

 

' Define the "property get" function to retrieve the Radius property

Property Get Radius() As Double

Radius = cirRadius

End Property

 

' Define the "property let" procedure to change the Radius value

Property Let Radius(val As Double)

cirRadius = val

End Property

 

Sub Draw

' Method performs some action here

End Sub

Scripts that use the class module would access the object as follows (assuming the module is stored in the file CIRCLE.CLS and that the object name entered in the Edit Object Module Properties dialog is MyCircleObject )

'#Uses "circle.cls"

Sub Main

Dim x As New MyCircleObject

x.Radius = 7 ' sets the value of the Radius property

x.Draw ' calls the Draw method

Debug.Print x.Radius ' prints 7 in the immediate window

End Sub

See Also

Creating Dialogs with Scripter