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

Developing Class Modules

                    Extending a Simple Class
                             One  of the advantages of using a class to hold related data is that you can easily extend the
                             class using several different techniques. For example, you can easily add a synonym for an
                             existing  property with a pair of property routines like this:

                             Public Property Get CommonName() As String

                             CommonName = Name
                                                                                                             Chapter 14
                             End Property
                             Public Property Let CommonName(value As String)
                             Name = value

                             End Property
                             These routines are used to return and modify a  public class-level variable, thus allowing the
                             user to manipulate the same value by using two different names.
                             Another useful technique is to add a  method that allows you to initialize all the properties of
                             the class with a single call. Notice that the following routine takes advantage of the  Me key-
                             word so that anyone using this method would know which parameter affects which property:

                             Public Sub Init(Name As String, _
                                 ScientificName As String, _
                                 Description As String, _
                                 RetailPrice As Currency, _
                                 WholesaleCost As Currency, _
                                 ProductNumber As Long)
                             Me.Name = Name
                             Me.ScientificName = ScientificName
                             Me.Description = Description
                             Me.RetailPrice = RetailPrice
                             Me.WholesaleCost = WholesaleCost
                             Me.ProductNumber = ProductNumber
                             End Sub

                    A Collection Class

                             It’s often useful to create a collection class to hold a group of objects. This task is made a lot
                             easier by using the Visual Basic Collection object to store your data. The following code
                             declares a  Collection object variable that’s local to the class. When the class is first instanti­
                             ated, the Collection object is created, and when the object is destroyed, the Collection object
                             will also be destroyed.





                                                                                                       313
                                                                                                Part 4:  Advanced VBA
   334   335   336   337   338   339   340   341   342   343   344