【问题标题】:Collection of Collections - How to make sub-collections by value rather than reference?Collection of Collections - 如何按价值而不是参考来制作子收藏?
【发布时间】:2017-04-06 08:33:00
【问题描述】:

我想要做的是 Autodesk Inventor。我正在编写一个程序,它遍历草图中的一堆线。它收集连接线组并将它们放在一个集合中。然后它使这些集合的集合被处理。

我试图通过将行添加到临时集合中,然后将此临时集合添加到循环集合中来做到这一点,这样我就不必为每个循环生成未知数量的集合。但是,一旦我使用 Clear 方法重置临时集合,它就会删除我刚刚推送到循环集合中的信息。有没有办法让循环集合中的信息独立于临时集合中的信息?

如您所见,问题是我永远不知道将连接多少条线路,因此我永远不知道会有多少子集合。

这是我的代码。

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

【问题讨论】:

  • 代码oLoopColl.Add(oSubColl, CStr(j))reference 添加到子集合到循环集合。这个引用只是指向同一个子集合,所以当子集合被清除时,循环集合指向这个现在是空的集合。您不能添加子集合by-value。您可以每次创建新集合,将子集合的元素复制到这个新集合中,并将这个新集合添加到循环集合中。如果您不清除此新集合,则循环集合将保留它。
  • 但是如何处理循环中可变数量的子集合呢?据我所知,我无法根据变量准确定义变量;即oLoop & i = oLoop1, oLoop2 for i = 1 to 2
  • 也许我只需要重新考虑我的方法,并且只在我进行需要它们作为 ObjectCollection 的调用之前将 oLoops 存储为 ObjectCollection ...即;将它们存储在数组中并将数组转换为 ObjectCollection。

标签: vba collections autodesk-inventor


【解决方案1】:

我在评论中想说的是以下内容。在示例中,您可以看到不必知道items 中的items 的数量container


当新的item 应该添加到container 时,创建一个:

Set item = New Collection

然后将items添加到这个新的item

item.Add "Some-New-Item"

最后将这个新的item 的引用添加到container

container.Add item

container 现在保存对item 所在内存位置的引用。因此可以添加下一项,然后添加下一项,依此类推。


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

输出:

Col_Item_1
Col_Item_11
Col_Item_111
Col_Item_2
Col_Item_222

why not use As New

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多