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

Microsoft Office Excel 2003 Programming Inside Out

                             For example, the following line defines a simple property:

                             Public ProductId As Long

                             Warning  Simple properties can’t be used to return an array, a fixed-length string, a con­
                             stant, or a complex structure created with a Type statement. If you need a property that
                             returns any of these things, create the appropriate private class-level variable and then cre­
                             ate a property routine that performs the same action. For example, you can create a prop­
                             erty routine that accepts a series of parameters that allow the routine to look like an array.
                             Likewise, you can create a property routine that accepts and returns fixed-length strings or
                             complex structures.

             Chapter 14
                    Defining Property Routines

                             There are three different property routines: Get, Set, and Let. The Property Get routine always
                             returns a value. Here’s a simple example that references a private class-level variable named
                             TheProductName.

                             Public Property Get ProductName As String
                             ProductName = TheProductName

                             End Property
                             The Property Let and Property Set routines are called to save a value into the property. The
                             Property Let statement is used when the property is a normal variable, whereas the Property
                             Set statement is used when the property is an object.
                             The corresponding Property Let routine for the ProductName property would look like this:

                             Public Property Let ProductName(value As String)

                             TheProductName = value
                             End Property


                             Note  The only difference between a Property Set statement and a Property Let statement
                             is that you use a Property Set statement whenever you are dealing with objects, whereas
                             you would normally assign values using the Set statement. Likewise, you would use the
                             Property Let statement whenever you are returning any other value.











                306
             Part 4:  Advanced VBA
   327   328   329   330   331   332   333   334   335   336   337