Class modules are used to create blueprints for new objects, such as the Employee object presented in this chapter:

□ Function and Sub procedures are used in the class module to create methods for the object.

□ Public variables declare the properties for the object.

□ However, if you need to take programmatic control when a property is assigned a value, you can define the property using a Property Let procedure.

□ In addition, Property Get procedures allow you to control access to property values.

To use the code in your class module, you create one or more instances of your object. For example, you can create Mary and Jack as instances of an Employee object. You can further customize your objects by creating your own collection, where you add all the instances of your object.

Class modules are not used to create objects to the same extent in Excel VBA as they are used in a standalone programming language such as Visual Basic. This is because Excel already contains the objects that most Excel programmers want to use. However, Excel programmers can use class modules to:

□ Trap application-level events, such as the WorkbookBeforePrint event that allows you to control the printing of all open workbooks

□ Trap events in embedded charts

□ Write a single event procedure that can be used by many instances of a particular object, such as a TextBox control on a UserForm

□ Encapsulate difficult code and make it easier to use

□ Encapsulate code so you can share the code among different projects and users See Chapter 27 for examples of encapsulation of API code.

0 0

Post a comment