创建嵌套类 [英] Creating Nested Class
本文介绍了创建嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在VBA中创建嵌套类.
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"
c部门类
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类
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.
公司类别:
Option Explicit
Private mDepartmentsList As Object
Public Property Get Department(ByVal StringKey As String) As Department
With mDepartmentsList
If Not .Exists(StringKey) Then
Dim objDepartment As New Department
.Add StringKey, objDepartment
End If
End With
Set Department = mDepartmentsList(StringKey)
End Property
Public Property Get Keys() As Variant
Keys = mDepartmentsList.Keys
End Property
Private Sub Class_Initialize()
Set mDepartmentsList = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set mDepartmentsList = Nothing
End Sub
部门类别:
Option Explicit
Private mEmployeesList As Object
Public Property Get Employee(ByVal StringKey As String) As String
Employee = mEmployeesList(StringKey)
End Property
Public Property Let Employee(ByVal StringKey As String, ByVal StringValue As String)
mEmployeesList(StringKey) = StringValue
End Property
Public Property Get Keys() As Variant
Keys = mEmployeesList.Keys
End Property
Private Sub Class_Initialize()
Set mEmployeesList = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set mEmployeesList = Nothing
End Sub
测试:
Option Explicit
Sub TestCompanyClass()
Dim OurCompany As Company
Set OurCompany = New Company
With OurCompany
.Department("Finance").Employee("John") = "Employee Number 100"
.Department("Finance").Employee("Kim") = "Employee Number 101"
.Department("Engineering").Employee("Sam") = "Employee Number 124"
End With
Dim d As Variant, e As Variant
With OurCompany
For Each d In .Keys
Debug.Print "Department: " & d
For Each e In .Department(d).Keys
Debug.Print vbTab & "Employee: " & e & " - " & .Department(d).Employee(e)
Next e
Next d
End With
Set OurCompany = Nothing
End Sub
输出:
Department: Finance
Employee: John - Employee Number 100
Employee: Kim - Employee Number 101
Department: Engineering
Employee: Sam - Employee Number 124
这篇关于创建嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文