c-- styles for logos and headline links do not modify internet, red, or black styles -->
|
|
|
|
|
|
|
|
By Paul Lomax, Matt Childs & Ron
Petrusha
The Class ConstructYou declare a class using the
where classname is the name you want to assign to the class. It must follow standard VBScript variable naming conventions. Classes can contain variables, properties, methods, and events. How many of these and of what types is completely up to you. It is possible to have an object that has no properties or methods and supports only the two default events, but it won't be a very useful class. To instantiate an object--that is, to create an instance of your class that you can use in your code--use the following syntax:
where oObj is the name you want to assign to your object variable (it again must follow standard VBScript variable naming conventions), and classname is the name of the class. The statement creates an object reference--that is, the variable oObj contains the address of your object in memory, rather than the object itself. Class VariablesIn addition to properties, methods (which are either functions
or subroutines), and events (which are subroutines), the code inside a
The variable name must once again follow standard VBScript variable naming conventions. The In general, it is poor programming practice to make a class variable visible outside of the class. There are numerous reasons for this, the most important of which is that you have no control over the value assigned to the variable (which is especially a problem when dealing with a weakly typed language like VBScript) and no ability to detect when the value of the variable has been changed. As a rule, then, all variables declared within your classes should be private. Class PropertiesTypically, class properties are used to "wrap" the private variables of a class. That is, to change the value of a private variable, the user of your class changes the value of a property; the property assignment procedure (called a Property Let procedure) handles the process of data validation and assigning the new value to the private variable. If the private variable is an object, use an object property assignment procedure (called a Property Set procedure) to assign the new property value to the private object variable. Similarly, to retrieve the value of a private variable, the user of your class retrieves the value of a property; the property retrieval procedure (called a Property Get procedure) handles the process of returning the value of the private variable. Read-only properties (which wrap read-only private variables) have only a Property Get procedure, while write-only properties (which are rare) have only a Property Let or a Property Set procedure. Otherwise, properties have a Property Get procedure and either a Property Let or a Property Set procedure and are read-write. The use of public properties that are available outside of the class to wrap private variables is illustrated in Example 2-5, which shows a simple class that defines a private variable, modStrType, and a two read-write properties, ComputerType and OperatingSystem, the latter of which is an object property. Normally, you would validate the incoming data in the Property Let and Property Set procedures before assigning it to private variables, although that hasn't been done here to keep the example as simple as possible. Example 2-5: Using Properties to Wrap Private Variables
Class MethodsMethods allow the class to do something. There is no magic to methods: they are simply subroutines or functions that do whatever it is you wish for the object to do. For example, if we created an object to represent a laptop computer in a company's inventory, then we would like to have a method that reports the laptop's owner. Example 2-6 shows a class with such a method. Example 2-6: Creating a Class Method
As with properties, you can use the Note that in Example 2-6, the Property Get procedure performs the same functionality as the GetOwner method. This is quite common: you often can choose whether you want to implement a feature as a property or as a method. In this case, you could define both property procedures to be private; then the only way for anyone to get the owner information from the object would be to invoke the GetOwner method. The GetOwner method is declared as a function because it returns a value to the calling code. You can write methods as subroutines as well. You would do this when the method that you are calling does not need to pass back a return value to the caller. Class EventsTwo events are automatically associated with every class you create: Class_Initialize and Class_Terminate. Class_Initialize is fired whenever you instantiate an object based on this class. The executing the statement:
causes the event to fire. You can use this event to set class variables, to create database connections, or to check to see if conditions necessary for the creation of the object exist. You can make this event handler either public or private, but usually event handlers are private--this keeps the interface from being fired from outside code. The general format of the Class_Initialize event is:
The Class_Terminate event is fired when the object goes out of
scope, or when the object is set to
Once again, the event handler can either be public or private, though ordinarily it's defined as private to prevent termination code from being executed from outside of the class. Note that the Class_Terminate event is fired only when every instance of an object is destroyed. So, for instance, in the code fragment:
the event is fired only in response to the last statement, when
oDog3 is set equal to |
|