VBA - 如何将集合添加到集合集合中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5116460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:10:17  来源:igfitidea点击:

VBA - How to add a collection to a collection of collections

vbaexcel-vbacollectionsword-vbaexcel

提问by Stevo

I am trying to create code to represent a form document using VBA in Word 2007. I have created classes to represent Section, QuestionSet and Question.

我正在尝试使用 Word 2007 中的 VBA 创建代码来表示表单文档。我创建了类来表示部分、问题集和问题。

So I have 15 Sections. I have created a function to create each 'Section' Object add it to the 'Sections' Collection then destroy the object, the result being that the objects remain persistent in the collection (or something).

所以我有15个部分。我创建了一个函数来创建每个“Section”对象,将其添加到“Sections”集合中,然后销毁该对象,结果是对象在集合(或其他东西)中保持持久性。

Is it possible to use the same method to add collections to collections or would I have to define each collection explictly?

是否可以使用相同的方法将集合添加到集合中,还是必须明确定义每个集合?

Code in Module:

模块中的代码:

Public Sections As Collection

Function DefineSection(ByVal SectionName As String)

    Set Section = New clsSection
    Section.myName = SectionName
    Sections.Add Section, SectionName

End Function


Function DefineQuestionSet(ByVal SectionName As String, ByVal Name As String, ByVal NoOfQuestions As Integer, ByVal IsMutuallyExclusive As Boolean, Optional ByVal DependentOnSection As String)

    Dim Qsets As Collection

    Set Qsets = New Collection
    Set QuestionSet = New clsQuestionSet

    QuestionSet.Name = Name
    QuestionSet.NoOfQuestions = NoOfQuestions
    QuestionSet.MutuallyExclusive = IsMutuallyExclusive

    If Not (DependentOnSection) = "" Then
        QuestionSet.DependentOnSection = DependentOnSection
    End If

    Qsets.Add QuestionSet
    Sections.Item(SectionName).Add Qsets

End Function

Then this is called via:

然后通过以下方式调用:

Sub Initilise()

    Set Sections = New Collection

    DefineSection "PersonalDetails"
    DefineQuestionSet "PersonalDetails", "PersonalDetails", 29, False

End Sub

采纳答案by mwolfe02

Yes. You can absolutely add collections to collections to collections ad infinitum. The code you have posted looks like it should work just from glancing through it. Are you having specific problems?

是的。您绝对可以无限地将集合添加到集合中。您发布的代码看起来应该可以通过浏览它来工作。你有具体的问题吗?

UPDATE: VBA only passes around referencesto objects. If you explicitly destroy an object after assigning it to a collection (eg, Set myObj = Nothing) then you will also be destroying the object inside the collection.

更新:VBA 只传递对对象的引用如果在将对象分配给集合后显式销毁对象(例如,Set myObj = Nothing),那么您也将销毁集合内的对象。

[EDIT]: Apparently this is not true. From this website(first linked by Stevo in the comments):

[编辑]:显然这不是真的。从这个网站(首先由 Stevo 在评论中链接):

In order to use collections to manage class objects, you must do the following:

为了使用集合来管理类对象,您必须执行以下操作:

  • Create an instance of the class
  • Set the properties and methods of the class
  • Add the class to a public collection
  • Unload the instance of the class
  • 创建类的实例
  • 设置类的属性和方法
  • 将类添加到公共集合
  • 卸载类的实例

You might expect that unloading the instance of the class results in the class being closed and terminated. However, the class object persists because you add it to a collection, which then owns the reference to the class. This is a very powerful technique that allows you to control object references through a collection; the class object does not terminate until you remove it from the collection.

您可能期望卸载类的实例会导致类被关闭和终止。但是,类对象会持续存在,因为您将其添加到集合中,然后该集合拥有对类的引用。这是一种非常强大的技术,它允许您通过集合来控制对象引用;类对象在您从集合中删除它之前不会终止。

UPDATE: There's no reason why you can't add a collection to an object. You just need the class your object is instantiated from to support such a method. For example, in your clsSectionclass module you need an Addmethod which adds objects passed to it to a collection stored in the clsSection:

更新:没有理由不能向对象添加集合。您只需要实例化对象的类来支持这种方法。例如,在您的clsSection类模块中,您需要一个Add方法将传递给它的对象添加到存储在以下位置的集合中clsSection

Private QSetsColl As Collection

Public Sub Add(QSets As Object)
    If QSetsColl Is Nothing Then Set QSetsColl = New Collection
    QSetsColl.Add QSets
End Sub

回答by daniele3004

Try this simple example:

试试这个简单的例子:

Private Sub CommandButton1_Click()

    Dim masterCollection As New collection
    masterCollection.Add "first key", createDetailCollection()
    masterCollection.Add "second key", createDetailCollection()
    masterCollection.Add "third key", createDetailCollection()

End Sub

The follow function return a initializated collection

以下函数返回一个初始化的集合

Function createDetailCollection()
    Dim collection As New collection
    createCollection = collezioneBuy
End Function

回答by Stevo

I think I have the answer now, but if someone could clarify that would be helpful.

我想我现在有了答案,但如果有人能澄清那会有所帮助。

I think what the code is doing is trying to add a collection to an object - which obviously won't work. So I need to create a collection which I can add the object AND the collection to. etc.

我认为代码正在做的是试图将一个集合添加到一个对象中——这显然是行不通的。所以我需要创建一个集合,我可以将对象和集合添加到其中。等等。