Using Property Let and Property Get to Control How Users Utilize Custom Objects

The Property Get EmpWeeklyHrs totals these hours and returns a value to this property. Without it, a value cannot be retrieved from EmpWeeklyHrs: Property Get EmpWeeklyHrs() As Double

EmpWeeklyHrs = NormalHrs + OverHrs

End Property

Property Get procedures are created for EmpNormalHrs and EmpOverTimeHrs to set their values. If you use Property Get procedures only, the values of these two properties are readonly. They can be assigned values only through the EmpWeeklyHrs property:

Property Get EmpNormalHrs() As Double EmpNormalHrs = NormalHrs End Property

Property Get EmpOverTimeHrs() As Double EmpOverTimeHrs = OverHrs End Property

Finally, the method EmpWeeklyPay is updated to reflect the changes in the properties and goal:

Public Function EmpWeeklyPay() As Double

EmpWeeklyPay = (EmpNormalHrs * EmpRate) + (EmpOverTimeHrs * EmpRate * 1.5) End Function

Update the procedure in the standard module to take advantage of the changes in the class module. The new message box resulting from this updated procedure is shown in Figure 20.8:

Sub EmpPayOverTime()

Dim Employee As New clsEmployee

With Employee

.EmpName = "Tracy Syrstad" .EmpID = "1651" .EmpRate = 25 .EmpWeeklyHrs = 45

MsgBox .EmpName & Chr(10) & Chr(9) & _ "Normal Hours: " & .EmpNormalHrs & Chr(10) & Chr(9) & _ "OverTime Hours: " & .EmpOverTimeHrs & Chr(10) & Chr(9) & _ "Weekly Pay : $" & .EmpWeeklyPay End With

Figure 20.8

Use Property Let and Property Get procedures for more control over custom object properties.

Microsoft Excel

Tracy Syrslad

Normal Hours: 40 OverTime Hours; 5 Weekly Pay: $1187.5

0 0

Post a comment