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

Developing Class Modules

                    A Class with Business Rules
                             You can also extend a class by adding basic business rules. For example, the following method
                             validates the information in a Plant object. This code merely checks each property in the class
                             with potential error conditions and returns True if no errors are found and False if they are.

                             Public Function IsValid() As Boolean

                             If Len(Name) = 0 Then
                                 IsValid = False
                                                                                                             Chapter 14
                             ElseIf Len(ScientificName) = 0 Then
                                 IsValid = False
                             ElseIf WholesaleCost < 0 Then
                                 IsValid = False

                             ElseIf RetailPrice < WholesaleCost Then
                                 IsValid = False

                             ElseIf ProductNumber < 0 Then
                                 IsValid = False
                             Else
                                 IsValid = True
                             End If

                             End Function
                             The IsValid procedure could be modified to return a text error message or even a String array
                             containing a list of errors found within the data.
                             Another way to detect errors is to use property routines. For instance, you could create a
                             Property Let routine like this one.

                             Public Property Let RetailPrice(value As Currency)

                             If value > WholesaleCost Then
                                 MyRetailPrice = value

                             Else
                                 RaiseEvent PlantError(1, “Retail price lower than the wholesale cost.”)
                             End If

                             End Property








                                                                                                       317
                                                                                                Part 4:  Advanced VBA
   338   339   340   341   342   343   344   345   346   347   348