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

发布于 2024-10-19 08:03:17 字数 1276 浏览 1 评论 0原文

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

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

是否可以使用相同的方法将集合添加到集合中,或者我是否必须显式定义每个集合?

模块中的代码:

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

然后通过以下方式调用:

Sub Initilise()

    Set Sections = New Collection

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

End Sub

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.

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).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-10-26 08:03:17

。您绝对可以无限地添加集合到集合到集合。您发布的代码看起来只需浏览一下就应该可以工作。您有具体问题吗?

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

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

为了使用集合来管理
类对象,你必须这样做
以下:

  • 创建类的实例
  • 设置类的属性和方法
  • 将类添加到公共集合
  • 卸载类的实例

您可能期望卸载
该类的实例结果为
课程已关闭并终止。
但是,类对象仍然存在
因为你将它添加到集合中,
然后拥有对
班级。这是一个非常强大的
允许您控制的技术
对象引用通过
收藏;类对象没有
终止,直到您将其从
收藏。

更新:没有理由不能将集合添加到对象中。您只需要实例化对象的类即可支持此类方法。例如,在 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

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 references to 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.

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

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 clsSection class module you need an Add method which adds objects passed to it to a collection stored in the 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
羁绊已千年 2024-10-26 08:03:17

试试这个简单的例子:

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

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

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

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
萤火眠眠 2024-10-26 08:03:17

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文