tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,937
- Office Version
- 365
- 2019
- Platform
- Windows
I have been going through classes and wondered if there's any point to it.
The following example is taken from the Mr. Excel Book, VBA And Macros For Microsoft Excel.
This is in module1:
This is in the class, clsEmployee:
[ASIDE: There's a slight quirk here in that if I step into the module, when I get to the Function EmpWeeklyPay, it jumps to the Property Get EmpOverTimeHrs first. I expected it to read from left to right and so should go to the Property EmpNormalHrs first].
My main question is, what's the diference if I did this?
Put this in module2:
This is in a new class, clsEmployeechanged:
It seems it does the same thing by simply changing the variables NormalHrs and OverHrs from Private to Public.
The following example is taken from the Mr. Excel Book, VBA And Macros For Microsoft Excel.
This is in module1:
Code:
Sub EmpPayOverTime()
Dim Employee As New clsEmployee
With Employee
.EmpName = "Joe Bloggs"
.EmpRate = 100
.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
End Sub
This is in the class, clsEmployee:
Code:
Public EmpName As String
Public EmpRate As Double
Private NormalHrs As Double
Private OverHrs As Double
Property Let EmpWeeklyHrs(Hrs As Double)
NormalHrs = WorksheetFunction.Min(35, Hrs)
OverHrs = WorksheetFunction.Max(0, Hrs - 35)
End Property
Property Get EmpWeeklyHrs() As Double
EmpWeeklyHrs = NormalHrs + OverHrs
End Property
Property Get EmpNormalHrs() As Double
EmpNormalHrs = NormalHrs
End Property
Property Get EmpOverTimeHrs() As Double
EmpOverTimeHrs = OverHrs
End Property
Public Function EmpWeeklyPay() As Double
EmpWeeklyPay = (EmpNormalHrs * EmpRate) + (EmpOverTimeHrs * EmpRate * 2)
End Function
[ASIDE: There's a slight quirk here in that if I step into the module, when I get to the Function EmpWeeklyPay, it jumps to the Property Get EmpOverTimeHrs first. I expected it to read from left to right and so should go to the Property EmpNormalHrs first].
My main question is, what's the diference if I did this?
Put this in module2:
Code:
Sub EmpPayOverTimeChanged()
Dim Employee As New clsEmployeechanged
With Employee
.EmpName = "Joe Bloggs"
.EmpRate = 100
.EmpWeeklyHrs = 45
MsgBox .EmpName & Chr(10) & Chr(9) & _
"Normal Hours: " & .NormalHrs & Chr(10) & Chr(9) & _
"OverTime Hours: " & .OverHrs & Chr(10) & Chr(9) & _
"Weekly Pay : $" & .EmpWeeklyPay
End With
End Sub
This is in a new class, clsEmployeechanged:
Code:
Public EmpName As String
Public EmpRate As Double
Public NormalHrs As Double
Public OverHrs As Double
Property Let EmpWeeklyHrs(Hrs As Double)
NormalHrs = WorksheetFunction.Min(35, Hrs)
OverHrs = WorksheetFunction.Max(0, Hrs - 35)
End Property
Property Get EmpWeeklyHrs() As Double
EmpWeeklyHrs = NormalHrs + OverHrs
End Property
Public Function EmpWeeklyPay() As Double
EmpWeeklyPay = (NormalHrs * EmpRate) + (OverHrs * EmpRate * 2)
End Function
It seems it does the same thing by simply changing the variables NormalHrs and OverHrs from Private to Public.