2017-06-13 6 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を取得します。

編集:下記は、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 

ブランチインフォクラス内のデータを簡単に抽出できるように、ブランチインフォクラスを参照する方法はありますか?

ありがとうございます!

Eeshwar

+0

ありますが、私は、あなたの質問に応答するために、クラスのプロパティとメソッドにコードなどせずに、それは難しいだろう。 –

+0

こんにちは、画像をインラインで含むように質問を更新していただきありがとうございます。 File_Infoクラス情報を追加しました。それが役に立てば幸い! – Eeshwar

答えて

0

私はFor ... eachループを使用してではなく、項目番号を参照するキーのリストを反復という点で違ったことを行います。ここでは、2つのレベルを使用して動作するスニペットです。

プロパティ値が配列に書き込まれる行は無視できますが、それらは元のコードの一部です。

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

最初の行は不要です。

cf.Dependentsは、あなたの質問に表示されるような配列では、ということcFamilyオブジェクト内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 

注意です。

0

IMO FileListBranchListの場合、単純なVBA.Collectionを使用することができます。この例ではListクラスはItemsおよびInfoクラスを持ち、Listを参照しています。ここで、Listはのラッパーです。 HTH

詳細については、以下をご覧ください。 here

の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 
関連する問題