Collection of Collections - How to make sub-collections by value rather than reference?

蓝咒 提交于 2021-01-28 19:24:43

问题


What I am trying to do is for Autodesk Inventor. I'm writing a program that goes iterates through a bunch of lines in a sketch. It gathers the groups of connected lines and puts them in a collection. Then it makes a collection of these collections to be processed.

I'm trying to do this by adding the lines to a temporary collection, then adding this temporary collection to the collection of loops, so that I don't have to generate an unknown amount of collections for each loop. However, as soon as I reset the temporary collection using the Clear method, it erases the information that I had just pushed into the collection of loops. Is there any way to make the information in the collections of loops independent of what is in the temporary collection?

As you can see, the issue is that I never know how many lines will be connected, so thus I never know how many sub collections there will be.

Here is the code I have.

Dim oLoopColl As New Collection
Dim oSubColl As ObjectCollection
Set oSubColl = ThisApplication.TransientObjects.CreateObjectCollection

For j = 1 To oSLColl.Count
    oSubColl.Add (oSLColl.Item(j))
    'Check for last item to see if it is part of the first
    If j = oSLColl.Count Then
        If oSLColl.Item(j).EndSketchPoint Is oSLColl.Item(1).StartSketchPoint Then
            MsgBox ("Last SL is part of first coll!")
            oLoopColl.Item(1).Add (oSLColl.Item(j))
            oSubColl.Clear
        Else
            Call oLoopColl.Add(oSubColl, CStr(j))
        End If
    Else
        If Not oSLColl.Item(j).EndSketchPoint Is oSLColl.Item(j + 1).StartSketchPoint Then
            Call oLoopColl.Add(oSubColl, CStr(j))
            oSubColl.Clear
        End If
    End If
Next
oSubColl.Clear
Set oSubColl = Nothing

回答1:


What I was trying to say in the comment was the following. In the example you can see that it is not necessary to know the number of items in the container.


When new item should be added to the container create one:

Set item = New Collection

Then add items to this new item

item.Add "Some-New-Item"

And finally add reference to this new item to the container

container.Add item

The container saves now the reference to the memory place where the item resides. And so next item can be added and then next one and so on.


Option Explicit

Private Const ColItem As String = "Col_Item_"

Sub Demo()
    Dim container As VBA.Collection
    Dim item As VBA.Collection

    Set container = New Collection

    Set item = New Collection
    item.Add ColItem & 1
    item.Add ColItem & 11
    item.Add ColItem & 111
    container.Add item

    Set item = New Collection
    item.Add ColItem & 2
    item.Add ColItem & 22
    item.Add ColItem & 222
    container.Add item

    ' Clear is not part of VBA-Collection so Remove-all could simulate it
    ' When Clear would be called here then all the items will be removed
    ' and the container will reference an empty collection
    item.Remove 2

    Dim outer, inner
    For Each outer In container
        For Each inner In outer
            Debug.Print inner
        Next inner
    Next outer
End Sub

Output:

Col_Item_1
Col_Item_11
Col_Item_111
Col_Item_2
Col_Item_222

See why not use As New.



来源:https://stackoverflow.com/questions/40745720/collection-of-collections-how-to-make-sub-collections-by-value-rather-than-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!