Property Procedures

If your properties are defined by public variables, they are read/write properties. They can be directly accessed and can be directly assigned new values, as you have seen in the previous section. If you want to perform checks or calculations on properties, you use the Property Let and Property Get procedures to define the properties in your class module, instead of using public variables.

Property Get procedures allow the class module to control the way in which properties are accessed. Property Let procedures allow the class module to control the way in which properties can be assigned values. You can also use Property Set procedures. They are similar to Property Let procedures, but they process objects instead of values.

For example, say you want to break up the employee hours into normal time and overtime, where the value of overtime is anything over 35 hours. You want to have an HoursPerWeek property, which includes both normal and overtime hours that can be read and can be assigned new values. You want the class module to split the hours into normal and overtime, and set up two properties, NormalHours and OverTimeHours, that can be read but cannot be directly assigned new values. You can set up the following code in the CEmployee class module:

Public Name As String Public Rate As Double

Private dNormalHrs As Double Private dOverTimeHrs As Double

'Return weekly pay

Public Function WeeklyPay() As Double

WeeklyPay = dNormalHrs * Rate + dOverTimeHrs * Rate * 1.5 End Function

'Convert input hours to normal and overtime Property Let HoursPerWeek(dHours As Double)

dNormalHrs = WorksheetFunction.Min(35, dHours) dOverTimeHrs = WorksheetFunction.Max(0, dHours - 35) End Property

'Return total hours per week Property Get HoursPerWeek() As Double

HoursPerWeek = dNormalHrs + dOverTimeHrs End Property

'Return normal hours

Property Get NormalHours() As Double

NormalHours = dNormalHrs End Property

'Return overtime hours

Property Get OverTimeHours() As Double

OverTimeHours = dOverTimeHrs End Property

HoursPerWeek is no longer declared as a variable in the declarations section. Instead, two new private variables have been added — dNormalHrs and dOverTimeHrs. HoursPerWeek is now defined by a Property Let procedure, which processes the input when you assign a value to the HoursPerWeek property. It breaks the hours into normal time and overtime. The Property Get procedure for HoursPerWeek returns the sum of normal and overtime hours when you access the property value.

NormalHours and OverTimeHours are defined only by Property Get procedures that return the values in the Private variables, dNormalHrs and dOverTimeHrs, respectively. This makes the properties NormalHours and OverTimeHours read-only. There is no way they can be assigned values except through the HoursPerWeek property.

The WeeklyPay function has been updated to calculate pay as normal hours at the standard rate and overtime hours at 1.5 times the standard rate. You can change the standard module code as follows to generate the message shown in Figure 16-3:

Sub EmployeePay()

Dim clsEmployee As CEmployee

'Create instance of CEmployee Set clsEmployee = New CEmployee

'Define properties clsEmployee.Name = "Mary" clsEmployee.Rate = 15 clsEmployee.HoursPerWeek = 45

'Display properties

MsgBox clsEmployee.Name & " earns $" _ & clsEmployee.WeeklyPay & "/wk" _ & " including " & clsEmployee.OverTimeHours & " hrs overtime"

End Sub

Figure 16-3

Figure 16-3

0 0

Post a comment