2017-06-13 70 views
1

我目前有使用下圖中顯示的數據結構存儲類的實例。每個-List項目是一個字典,每個-Info項目是一個類的實例。Excel VBA:有沒有辦法引用存儲在字典中的類的實例?

Data Structure

我讀其他地方,如果你Set一個實例變量等於另一個實例,它只是引用原始實例。它是否正確?

我已經能夠使用以下代碼爲fileInfo(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

+0

有,但沒有代碼的類屬性和方法,這將是我很難對你的問題作出迴應。 –

+0

嗨羅恩,感謝您更新的問題,包括圖像內聯。我已經添加了File_Info類的信息。希望能幫助到你! – Eeshwar

回答

0

我,我通過按鍵列表使用For ... each循環而不是指一個項目編號重複做不同的事情。這是一個使用兩個級別的片段。

您可以忽略屬性值寫入數組的行,但它們是原始代碼的一部分。

cf.DependentscDependentscFamily對象中的字典

'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) 

第一行是不必要的。

0

IMO有可能使用簡單的VBA.Collection,這裏舉例爲FileListBranchList。在該示例中,List類具有ItemsInfo類具有參考List,其中ListVBA.Collection的包裝。 HTH

欲瞭解更多閱讀材料, here

文件清單類

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 
相關問題