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

Developing Class Modules

                             Set ObjectVariable1 = New MyClass
                             Set ObjectVariable2 = ObjectVariable1
                             Set ObjectVariable1 = Nothing

                    Objects with Multiple Object Variables

                             It’s important to keep in mind that an object is not the same thing as an object variable. For
                             example, the following code creates an object, which has two variables pointing to it:

                             Set ObjectA = New MyClass
                             Set ObjectB = ObjectA
                             The first Set statement creates a new instance of MyClass, whereas the second Set statement   Chapter 14
                             merely creates a second pointer to the same object created by the first statement.

                             This means that the following statements will do the same thing because both ObjectA and
                             ObjectB point to the same object:

                             ObjectA.Name = “Roses"
                             ObjectB.Name = “Roses”

                             If this isn’t confusing enough, executing the following statement will not destroy the object.

                             Set ObjectA = Nothing
                             Because ObjectB still points to the object, it will remain in memory until ObjectB is also set
                             to Nothing.

                    Properties, Methods, and Events

                             Associated with every object is a collection of properties, methods, and events, which are
                             used to communicate information between the object and the routine that created the object.


                    Public vs. Private Properties, Methods, and Events

                             The individual parts of a class can be labeled as either Public or Private. Anything marked as
                             Public is visible to anyone using the class, whereas anything marked as Private can be accessed
                             only from the code within the class.


                             Tip  Don’t Rely on Defaults
                             Always explicitly mark everything within a class as either Public or Private, so you never
                             have to worry about whether something defaulted to Public or Private.









                                                                                                       303
                                                                                                Part 4:  Advanced VBA
   324   325   326   327   328   329   330   331   332   333   334