Excel VBA:有没有办法引用存储在字典中的类的实例?

我目前有使用下图中显示的数据结构存储类的实例。 每个-List项目是一个字典,每个-Info项目是一个类的一个实例。

数据结构

我读别处,如果你Set一个实例variables等于另一个实例,它只是引用原始实例。 它是否正确?

我已经能够使用下面的代码创buildfileInfo(1) (在图像中)的引用。

 Dim prflInfo As File_Info Set prflInfo = New File_Info Set prflInfo = fileList.Items(0) 

我试图使用下面的代码来引用branchInfo实例,但是我得到一个Run-time error 13: Type mismatch当我尝试这样做。

 Dim prbrInfo As Branch_Info With prflInfo Set prbrInfo = New Branch_Info brKey = .getbrKey(0) Set prbrInfo = .getbrItem(brKey) End With 

编辑:下面包含的是File_Info类的代码。 所有其他类都遵循这个基本模型。

 'Class Module: File_Info 'Initialise class variables Private pfileID As Integer Private pfilePath As String Private pfileName As String Private pbranchList As Scripting.Dictionary 'Declare variantcopy subroutine Private Declare Sub VariantCopy Lib "OleAut32" (pvarDest As Any, pvargSrc As Any) Private Sub Class_Initialize() Set pbranchList = New Scripting.Dictionary End Sub Public Property Let fileID(pfileIDi As Variant) pfileID = pfileIDi End Property Public Property Get fileID() As Variant fileID = pfileID End Property Public Property Let filePath(pfilePathi As Variant) pfilePath = pfilePathi End Property Public Property Get filePath() As Variant filePath = pfilePath End Property Public Property Let fileName(pfileNamei As Variant) pfileName = pfileNamei End Property Public Property Get fileName() As Variant fileName = pfileName End Property Public Sub addbrConn(branch As Branch_Info) pbranchList.Add branch.branchID, branch.brConn Debug.Print "addbrConn ID: " & branch.branchID End Sub Public Sub addBranch(branch As Branch_Info) pbranchList.Add branch.branchID, branch Debug.Print pbranchList.Count End Sub Public Function countbrList() countbrList = pbranchList.Count End Function Public Function getbrKey(Key As Variant) getbrKey = pbranchList.Keys(Key) End Function Public Function getbrItem(Key As Variant) getbrItem = GetByRefVariant(pbranchList.Items(Key)) End Function Public Sub dpbrList() With pbranchList Debug.Print pbranchList.Count For k = 1 To pbranchList.Count Debug.Print .Keys(k - 1), .Items(k - 1) Next k End With End Sub Public Sub updbrList(branch As Branch_Info) Dim branchID As String branchID = branch.branchID If pbranchList.exists(branchID) Then pbranchList.Remove (branchID) pbranchList.Add branchID, branch Debug.Print "Complete: " & branchID & " added." Else Debug.Print "Error: " & branchID & "does not exist." End If End Sub Private Function GetByRefVariant(ByRef var As Variant) As Variant VariantCopy GetByRefVariant, var End Function 

有没有办法引用branchInfo类,使其更容易提取其中的数据?

谢谢!

Eeshwar

我做了不同的事情,我使用For ... each循环遍历键列表,而不是引用项目号。 这是一个使用两个层次的代码片段。

您可以忽略将属性值写入数组的行,但它们是原始代码的一部分。

cf.DependentscFamily对象中cDependents的字典

 'Declarations in Main Module Dim dF As Dictionary, cF As cFamily, cD As cDependents Dim I As Long, J As Long Dim V As Variant, W As Variant ... For Each V In dF I = I + 1 Set cF = dF(V) With cF vRes(I, 1) = .FirstName vRes(I, 2) = .LastName vRes(I, 3) = .Birthdate J = 2 For Each W In .Dependents J = J + 2 Set cD = .Dependents(W) With cD vRes(I, J) = .Relation vRes(I, J + 1) = .DepName End With Next W End With Next V 

请注意,在您的问题中显示的顺序如下:

 set Obj = new Obj set Obj = myClass(0) 

第一行是不必要的。

国际海事组织是可以使用简单的VBA.Collection ,这里是FileListBranchList 。 在这个例子中, List类具有ItemsInfo类对List引用,其中ListVBA.Collection包装。 HTH

更多的阅读看看这里 。

FileList类

 Option Explicit Private m_fileInfoCollection As FileInfoCollection Private Sub Class_Initialize() Set m_fileInfoCollection = New FileInfoCollection End Sub Public Property Get Items() As FileInfoCollection Set Items = m_fileInfoCollection End Property 

FileInfo类

 Option Explicit Private m_branchList As BranchList Private m_fileID As Integer Private Sub Class_Initialize() Set m_branchList = New BranchList End Sub Public Property Get FileID() As Integer FileID = m_fileID End Property Public Property Let FileID(ByVal vNewValue As Integer) m_fileID = vNewValue End Property Public Property Get BranchList() As BranchList Set BranchList = m_branchList End Property 

FileInfoCollection类

 Option Explicit Private m_collection As VBA.Collection Private Sub Class_Initialize() Set m_collection = New VBA.Collection End Sub Public Sub Add(ByVal newItem As FileInfo) m_collection.Add newItem, CStr(newItem.FileID) End Sub Public Function ItemByKey(ByVal key As String) As FileInfo Set ItemByKey = m_collection(key) End Function Public Function ItemByIndex(ByVal index As Long) As FileInfo Set ItemByIndex = m_collection(index) End Function Public Function Count() As Long Count = m_collection.Count End Function 

BranchList类

 Option Explicit Private m_branchInfoCollection As BranchInfoCollection Private Sub Class_Initialize() Set m_branchInfoCollection = New BranchInfoCollection End Sub Public Property Get Items() As BranchInfoCollection Set Items = m_branchInfoCollection End Property 

BranchInfo类

 Option Explicit Private m_branchID As Integer Public Property Get branchID() As Integer branchID = m_branchID End Property Public Property Let branchID(ByVal vNewValue As Integer) m_branchID = vNewValue End Property 

BranchInfoCollection类

 Option Explicit Private m_collection As VBA.Collection Private Sub Class_Initialize() Set m_collection = New VBA.Collection End Sub Public Sub Add(ByVal newItem As BranchInfo) m_collection.Add newItem, CStr(newItem.branchID) End Sub Public Function ItemByKey(ByVal key As String) As BranchInfo Set ItemByKey = m_collection(key) End Function Public Function ItemByIndex(ByVal index As Long) As BranchInfo Set ItemByIndex = m_collection(index) End Function Public Function Count() As Long Count = m_collection.Count End Function 

标准模块

 Option Explicit Sub Demo() ' Fill Dim bi As BranchInfo Set bi = New BranchInfo bi.branchID = 111 Dim fi As FileInfo Set fi = New FileInfo fi.FileID = 222 fi.BranchList.Items.Add bi Dim fl As FileList Set fl = New FileList fl.Items.Add fi ' Get Dim fi1 As FileInfo Set fi1 = fl.Items.ItemByIndex(1) Dim bi1 As BranchInfo Set bi1 = fi1.BranchList.Items(1) End Sub