Code Snippets


Collections

Dim colEmployees As New Collection 

Public Sub CreateNewEmployees()
Dim objEmployee As CEmployee

   Call RemoveAll

   Set objEmployee = New CEmployee
   objEmployee.Name = "Russell"
   objEmployee.Department = "Research"
   objEmployee.HourlyRate = 12.75
   objEmployee.HoursPerWeek = 35
   Call colEmployees.Add(Item:=objEmployee, key:=objEmployee.Name)

   Set objEmployee = New CEmployee
   objEmployee.Name = "Matthew"
   objEmployee.Department = "Sales"
   objEmployee.HourlyRate = 15
   objEmployee.HoursPerWeek = 45
   Call colEmployees.Add(Item:=objEmployee, key:=objEmployee.Name)

   Call MsgBox("Total Number of Employees is : " & colEmployees.Count)
   Call MsgBox("Employee(2) earns £" & colEmployees(2).WeeklyPay & " a week")
   Call MsgBox("Employee 'Matthew' earns £" & colEmployees.Item("Matthew").WeeklyPay & " a week")
   
   For Each objEmployee In colEmployees
      Call MsgBox("Employee '" & objEmployee.Name & " earns £" & objEmployee.WeeklyPay & " a week")
   Next objEmployee

   Set objEmployee = Nothing
End Sub

Private Sub RemoveAll
   Dim lcount As Long
   For lcount = 1 to colEmployees.Count
      ColEmployees.Remove 1
   Next lcount
End Sub



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrev