在这里,你可以创建对象模型类是这样的:
Company -> has Departments -> Department -> has Employees -> Employee
要创建像Departments
和包装类可能被认为是无意义的,但考虑到VBA.Collection
可以容纳任何事物,而不仅仅是Department
或Employee
的实例,因此集合包装器确保该集合只保存某种类型的对象。
Dim col As VBA.Collection
Set col = New VBA.Collection
col.Add 123, CStr(123)
col.Add Range("A1:C3"), "Range(""A1:C3"")"
col.Add "banana", "banana"
Dim wing As Employee
Set wing = New Employee
wing.Id = 200
wing.Name = "Wing"
col.Add wing, CStr(wing.Id)
Debug.Print col.Count ' Prints 4
简单的例子,HTH。
公司
Private m_departmets As Departmets
Public Property Get Departmets() As Departmets
Set Departmets = m_departmets
End Property
Private Sub Class_Initialize()
Set m_departmets = New Departmets
End Sub
部门
Private m_items As VBA.Collection
Private Sub Class_Initialize()
Set m_items = New VBA.Collection
End Sub
Public Sub AddItem(newItem As Department)
m_items.Add newItem, newItem.Name
End Sub
Public Function GetItem(Name As String) As Department
Set GetItem = m_items(Name)
End Function
部
名
Private m_name As String
Private m_employees As Employees
Public Property Get Name() As String
Name = m_name
End Property
Public Property Let Name(ByVal vNewValue As String)
m_name = vNewValue
End Property
Public Property Get Employees() As Employees
Set Employees = m_employees
End Property
Private Sub Class_Initialize()
Set m_employees = New Employees
End Sub
员工
Private m_items As VBA.Collection
Private Sub Class_Initialize()
Set m_items = New VBA.Collection
End Sub
Public Sub AddItem(newItem As Employee)
m_items.Add newItem, VBA.CStr(newItem.Id)
End Sub
Public Function GetItem(Id As Long) As Employee
Set GetItem = m_items(VBA.CStr(Id))
End Function
员工
Private m_name As String
Private m_id As Long
Public Property Get Name() As String
Name = m_name
End Property
Public Property Let Name(ByVal vNewValue As String)
m_name = vNewValue
End Property
Public Property Get Id() As Long
Id = m_id
End Property
Public Property Let Id(ByVal vNewValue As Long)
m_id = vNewValue
End Property
测试
Sub Test()
Dim john As Employee
Dim kim As Employee
Dim sam As Employee
Dim financeDepartment As Department
Dim engineeringDepartment As Department
Dim ourCompany As Company
Set john = New Employee
Set kim = New Employee
Set sam = New Employee
john.Name = "John"
john.Id = 100
kim.Name = "Kim"
kim.Id = 101
sam.Name = "Sam"
sam.Id = 124
Set financeDepartment = New Department
Set engineeringDepartment = New Department
financeDepartment.Name = "Finance"
engineeringDepartment.Name = "Engineering"
financeDepartment.Employees.AddItem john
financeDepartment.Employees.AddItem kim
engineeringDepartment.Employees.AddItem sam
Set ourCompany = New Company
ourCompany.Departmets.AddItem financeDepartment
ourCompany.Departmets.AddItem engineeringDepartment
Debug.Print ourCompany.Departmets.GetItem("Finance").Employees.GetItem(100).Name
Debug.Print ourCompany.Departmets.GetItem("Finance").Employees.GetItem(101).Name
Debug.Print ourCompany.Departmets.GetItem("Engineering").Employees.GetItem(124).Name
' Change name of Sam to Samuel
ourCompany.Departmets.GetItem("Engineering").Employees.GetItem(124).Name = "Samuel"
Debug.Print ourCompany.Departmets.GetItem("Engineering").Employees.GetItem(124).Name
End Sub
输出
John
Kim
Sam
Samuel
来源
2017-07-07 14:57:36
dee
这是很清楚你的问题是什么。请再扩展一些。 – Vegard
你显示为'cDeparment Class'似乎是'cCompany class',不是吗? –
我认为编辑简化 –