I am trying to create a nested class in VBA.
So far I have successfully created the following:
OurCompany.Department.Employee("John")
How can I create a few groups of Department so I can store the data seperately. Something like this
OurCompany.Department("Finance").Employee("John") = "Employee Number 100"
OurCompany.Department("Finance").Employee("Kim") = "Employee Number 101"
OurCompany.Department("Engineering").Employee("Sam") = "Employee Number 124"
cDeparment Class
Private pDepartmentEmployee As Collection
Public Property Get Department(RefString As String) As cEmployee
Set Department = pDepartment.Item(RefString)
End Property
Public Property Set Department(RefString As String, ByVal objDepartEmployee As cEmployee)
pDepartmentEmployee.Add objDepartEmployee, RefString
End Property
cEmployee Class
Private pEmployee As Collection
Public Property Get Employee(RefKey As String) As String
Employee = pEmployee.Item(RefKey)
End Property
Public Property Let Employee(RefKey As String, RefItem As String)
pEmployee.Add Item:=RefItem, Key:=RefKey
End Property
I strongly suggest to read the answer in this post including any attached references.
Nevertheless, a simple implementation could be as follows.
Company Class:
Department Class:
Implementation:
Output:
Here you could create object model with classes like this:
To create wrapper classes like
Departments
andEmployees
may seen to be purposeless but consider the fact that theVBA.Collection
can hold anything not just only instances ofDepartment
orEmployee
so this way the collection wrapper ensures, that the collection holds only objects of certain type.Simple example, HTH.