MACRO LIBRARY

Class example using collection

Free public access. No account or subscription required.

Files

Class example using collection

Code

Modules_3dvision

'This demonstrates how to use classes in VBA
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'After running, see immediate window
    
Enum lngDept
    Administrative = 1
    Sales = 2
    Technical = 3
End Enum
    
Sub main()
    Dim o3DV As Collection
    Dim employee As clsEmployee
    Dim str3DV(6) As String
    
    Set o3DV = New Collection
    
    str3DV(0) = "Todd"
    str3DV(1) = "Scott"
    str3DV(2) = "Kathy"
    str3DV(3) = "Carrie"
    str3DV(4) = "Barry"
    str3DV(5) = "Scott"
    str3DV(6) = "Keith"
    
    For i = 0 To UBound(str3DV)
        Set employee = New clsEmployee
        employee.Name = str3DV(i)
        If i <= 2 Then
            employee.Dept = lngDept.Administrative
            employee.Salary = 100000
        ElseIf 3 <= i And i <= 4 Then
            employee.Dept = lngDept.Sales
            employee.Salary = 50000
        ElseIf i >= 5 Then
            employee.Dept = lngDept.Technical
            employee.Salary = 45000
        End If
        o3DV.Add employee
    Next i
    
    Debug.Print "Collection size: " & o3DV.Count
    For Each employee In o3DV
        Debug.Print employee.Name
        Debug.Print employee.Dept
        Debug.Print employee.Salary
    Next employee
End Sub

Class Modules: clsEmployees

Private strName As String
Private dblSalary As Double
Private strDept As String
    
'Name
Public Property Get Name() As String
    Name = strName
End Property
Public Property Let Name(value As String)
    strName = value
End Property
    
'Department
Public Property Get Dept() As String
    Dept = strDept
End Property
Public Property Let Dept(value As String)
    strDept = value
End Property
    
'Salary
Public Property Get Salary() As Double
    Salary = dblSalary
End Property
Public Property Let Salary(value As Double)
    dblSalary = value
End Property