MACRO LIBRARY

Lesson 1.7 - classes

Free public access. No account or subscription required.

Files

Lesson 1.7 code

Code

Modules: classes1

Dim Thomas As cEmployee
    
Sub main()
    Set Thomas = New cEmployee
    Thomas.position = "engineer"
    Thomas.salary = 1000
    Thomas.PrintPaycheck
    Thomas.Summary
End Sub

Class Modules: cEmployee

Dim strPosition As String
Dim curSalary As Currency
    
'POSITION PROPERTY
Public Property Get position() As String
    position = strPosition
End Property
    
Public Property Let position(value As String)
    strPosition = value
End Property
    
'SALARY PROPERTY
Public Property Get salary() As Currency
    salary = curSalary
End Property
    
Public Property Let salary(value As Currency)
    If value < 0 Then
        MsgBox "Positive numbers only for salary."
        End
    Else
        curSalary = value
    End If
End Property
    
'WITHHOLDING TAX PROPERTY
    
Private Property Get tax() As Currency
    If curSalary < 30000 Then
        tax = curSalary * 0.2
    Else
        tax = curSalary * 0.25
    End If
End Property
    
'PRINT PAYCHECK FUNCTION
Public Function PrintPaycheck()
    If curSalary = 0 Or strPosition = "" Then
        MsgBox "Please provide a position and salary."
        End
    Else
        MsgBox "Paycheck printed!"
    End If
End Function
    
'SUMMARY SUB PROC
Public Sub Summary()
    MsgBox "Salary: " & curSalary & vbCrLf & "Tax withheld: " & tax
End Sub