Page 335 - Microsoft Office Excel 2003 Programming Inside Out
P. 335

Developing Class Modules


                             The same situation occurs when you execute the second statement. Because a value
                             wasn’t assigned to Latitude in the temporary variable, the previous value of 47.63 is over-
                             written by zero, which undoes the change made in the first statement.
                             There are a couple of ways to avoid this problem. The first and probably best way is to cre­
                             ate a class rather than use a Type statement. However, if you really want to use the Type
                             statement, you should create a temporary variable of your own, assign the values to the
                             structure, and then assign the structure to the property like this:
                             Dim TempVar As MapCoordinateType
                             TempVar.Latitude = 47.63                                                        Chapter 14
                             TempVar.Longitude = 122.13
                             MicrosoftWay.MapCoordinate = TempVar


                    Defining Methods

                             Methods are simply public functions and subroutines. They are free to accept any set of
                             parameters and return any type of value. They can also access any class-level variable whether
                             it’s public or private, along with any property routine.
                             For example, assume that the class held information about a particular product from the
                             Garden Company. You might create a function that computes the discounted price like this:

                             Public Function DiscountedPrice (Discount As Currency) As Currency

                             If Discount >= 0 and Discount < 1.0 Then
                                 DiscountedPrice = MyListPrice * (1 - Discount)
                             Else
                                 DiscountedPrice = MyListPrice
                             End If

                             End Function
                             This routine verifies that the input parameter is valid by making sure that it’s in the range of
                             0 to 1 and then computes the discount price accordingly. If the discount value is illegal, the
                             list price of the item is returned.

                             Tip  Saving Cycles
                             If you have a choice between using a property routine or a private class-level variable in a
                             method, use the private class-level variable. Doing that avoids the extra processor cycles
                             and memory required by the property routine and helps to speed up your application.








                                                                                                       309
                                                                                                Part 4:  Advanced VBA
   330   331   332   333   334   335   336   337   338   339   340