在這裏,你可以創建對象模型類是這樣的:
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',不是嗎? –
我認爲編輯簡化 –