另一个“ cBeam”类实例的字典作为“ cNode”类实例中的变量

时间:2019-02-28 05:03:29

标签: vba class

当我执行以下子代码时,我将实例化cNode类,在其中实例化另一组cBeam类。 cBeam的实例存储在vBeamList部分中创建的字典Private Sub Initialize()中。我使用Debug.Print检查了值,并且工作正常。但是,当它输入Public Property Get Beam(MemberNo As Long) As cBeam时,表明vBeamList(MemberNo)为空!

Sub Main()
    Dim dandelion As Object
    Dim NodeConnectivityDict As Object
    Dim NodeListDict As Object
    Set NodeListDict = CreateObject("Scripting.Dictionary")

    '1. Creates dictionary of Nodes and connected beams | Node: [Node, b1,b2,b3, , ]
    Call SelectNodeConnection(dandelion)
    '2. Cleans the empty elements of beam array in #1 and adds end index as multiarray. | Node:[[b1,0],[b2,1],[b3,1]]
    Set NodeConnectivityDict = CleanNodeDictAndGetBeamConnectivity(dandelion)
    '3. Get the list of all beams connected to node and instantiate a class that shows the Node and beam with the
    'LC stored in them
    Call DevelopInstancesNodeBeamLC(NodeConnectivityDict, NodeListDict)


    Debug.Print NodeListDict(58).Beam(501).ID **<-When the error showsup**
End Sub

Sub DevelopInstancesNodeBeamLC(NodeConnectivityDict, NodeListDict)
    Dim nodeInstance As cNode
    For Each varKey In NodeConnectivityDict.keys():
        Set NodeListDict(varKey) = New cNode
        NodeListDict(varKey).Initialize varKey, NodeConnectivityDict(varKey)
    Next
End Sub

cNode类

Private vID As Long
Private vBeamArray() As Variant
Private vNumBeams As Integer
Private vBeamList As Object


Private Sub Class_Initialize()
    Set vBeamList = CreateObject("Scripting.Dictionary")
End Sub

Public Sub Initialize(Node, BeamList)

    vID = Node
    vBeamArray = BeamList
    vNumBeams = UBound(vBeamArray) - LBound(vBeamArray) + 1

    For Each i In vBeamArray:
        vBeamList.Add i(0), New cBeam
        vBeamList(i(0)).Initialize i(0), i(1)
    Next

End Sub

Public Property Get ID() As Long
    ID = vID
End Property

Public Property Get NumberOfBeams() As Integer
    NumberOfBeams = vNumBeams
End Property

Public Property Get Beam(MemberNo As Long) As cBeam
    Set Beam = vBeamList(MemberNo)
End Property

Private Sub Class_Terminate()
End Sub

cBeam类

Private vID As Long
Private vConnectivity As Long

Private Sub Class_Initialize()
End Sub

Public Sub Initialize(BeamID, Connectivity)
    vID = BeamID
    vConnectivity = Connectivity
End Sub

Public Property Get ID() As Long
ID = vID
End Property

Public Property Get Connectivity() As Long
Connectivity = vConnectivity
End Property

Private Sub Class_Terminate()
End Sub

我的想法已经用完了。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

使用您的Main子项的这个稍加修改的版本(没有其他更改),我得到了预期的输出。

Sub Main()

    Dim NodeConnectivityDict As Object
    Dim NodeListDict As Object

    Set NodeConnectivityDict = CreateObject("Scripting.Dictionary")
    Set NodeListDict = CreateObject("Scripting.Dictionary")

    NodeConnectivityDict.Add 33, Array(Array(10, 20), Array(30, 40), Array(50, 60))
    NodeConnectivityDict.Add 58, Array(Array(1, 2), Array(3, 4), Array(501, 502))

    DevelopInstancesNodeBeamLC NodeConnectivityDict, NodeListDict

    Debug.Print NodeListDict(33).Beam(30).ID            '30
    Debug.Print NodeListDict(33).Beam(30).Connectivity  '40

    Debug.Print NodeListDict(58).Beam(501).ID           '501
    Debug.Print NodeListDict(58).Beam(501).Connectivity '502

End Sub