Microsoft Office Tutorials and References
In Depth Information
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 Tr ue if no errors are found and False if they are.
Public Function IsValid() As Boolean
If Len(Name) = 0 Then
IsValid = False
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
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
RaiseEvent PlantError(1, “Retail price lower than the wholesale cost.”)
End If
End Property
Search JabSto ::

Custom Search