Better Smarter Object

You get what you pay for, and creating object properties by simply declaring public variables inside a class module is a good example. It's quick and easy but doesn't give you much in return. On the other hand, you can create object properties using Property Get/Let/Set procedures. These types of procedures take a little more effort to set up, but they provide you with a lot more flexibility.

Property Get/Let/Set procedures give you two powerful capabilities. First, when you create an object property using Property Get/Let/Set, you can decide whether the property is read/write, readonly, or write-only by implementing one or two procedures (see Table 11.1). A Property Get procedure provides read access to a property. A Property Let/Set procedure provides write access to a property. You use Property Let when the property's data type is one of the basic data types (Integer, String, Variant, etc.) and Property Set when the underlying data type is an object (Worksheet, Range, etc.).

Table 11.1: Implementing Read/Write Combinations with Properties Read/Write Capability implementation

Read/Write Property Get/Property Let

Read-only Property Get

Write-only Property Let

Second, when you use Property procedures, your class is aware of changes made to a property value. If you use public variables, you have no way of knowing when a consumer procedure changes a given property's value. When you use Property Let to define write-access to a property, the only way to change the property's value is to go through the Property Let procedure. This allows you to do things like perform data validation.

To demonstrate how to use Property procedures, let's create a Loan object that has the same properties as SimpleLoan but uses Property procedures rather than public variables. Go ahead and insert a new class module into your VBA project and name it Loan.

I prefer to add stubs for all of the Property procedures before adding any of the implementation details. By stub, I'm referring to the opening and closing statement of each property. Let's go ahead and add all of the stubs.

1. Select Insert ^ Procedure from the VBE menu.

2. Enter PrincipalAmount in the Name text box.

3. Select Property as the Type.

4. Select Public as the Scope and click OK.

5. Repeat steps 1—4 for interest rate (InterestRate), term (Term), payment (Payment), and loan number (LoanNumber). Figure 11.4 shows a picture of the VBE after I've added the stubs for the PrincipalAmount property.

So now you have stubs for all of the properties. You need to get a little more specific regarding your specification before you go any further. Although you may consider what the loan payment will be when you're deciding on how much to borrow and for how long, ultimately the loan payment is a function of the amount you borrow, the term or length of the loan (or number of payments), and the interest rate. Therefore, it makes sense to make this a calculated amount and not an amount that you can change—it's a read-only property. As you recall, properties can be read/write, read-only, or write only. In order to make the Payment property read-only, go ahead and delete the Property Let stub for the Payment property. As an alternative, consider commenting out the Property Let stub in case you decide to add it back later. This is a good idea because it makes it clear that you're intending to implement a read-only property.

' this stub should be deleted or commented out Public Property Let Payment(ByVal vNewValue As Variant)

End Property

After the stubs are all ready, it's time to add some private module-level variables to hold the value associated with each property. The idea behind using private class variables is that it provides a level of abstraction and ensures that all class implementation details are hidden. Enter the following code underneath the Option Explicit statement at the top of the class module (you are using Option Explicit, right?).

0 0

Post a comment