Table of contents
TOC
折叠目录
展开目录

集合对象Collection Object

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

集合对象是一组有序的项目可以作为一个单元来引用的。备注集合对象提供了简便的方法来指代一组作为单个对象相关的项目。成员在集合中的项,只需要相关集合中存在的事实。集合中的成员不需要具有相同的数据类型。可以创建其他对象相同的方式创建一个集合。例如:A Collection object is an ordered set of items that can be referred to as a unit. Remarks The Collection object provides a convenient way to refer to a related group of items as a single object. The items, ormembers, in a collection need only be related by the fact that they exist in the collection. Members of a collection don't have to share the same data type. A collection can be created the same way other objects are created. For example:

Dim X As New Collection

创建集合后,可以使用Add方法,使用Remove方法移除添加成员。从集合时可以使用每个循环访问整个集合,则使用Item方法,可以返回特定成员...下一步语句。Once a collection is created, members can be added using the Add method and removed using the Remove method. Specific members can be returned from the collection using the Item method, while the entire collection can be iterated using the ForEach...Next statement.

示例Example

本示例创建一个集合对象 ( MyClasses),然后创建一个对话框,在其中用户可以将对象添加到集合。要观察其工作方式,从插入菜单中选择类模块命令,并声明一个公共变量,称为InstanceName的 Class1 模块级 (键入公用InstanceName)来保存的每个实例的名称。作为 Class1 保留默认名称。复制并将下面的代码粘贴到另一个模块中的常规部分,然后启动的语句), ClassNamer,然后创建一个对话框,在其中用户可以将对象添加到集合。要观察其工作方式,从插入菜单中选择类模块命令,并声明一个公共变量,称为InstanceName的 Class1 模块级 (键入公用InstanceName)来保存的每个实例的名称。作为 Class1 保留默认名称。复制和将以下代码粘贴到常规部分的另一个模块中,然后启动它的语句ClassNamer在另一个过程。(本示例仅可用于支持类的宿主应用程序。This example creates a Collection object ( MyClasses), and then creates a dialog box in which users can add objects to the collection. To see how this works, choose the Class Module command from the Insert menu and declare a public variable called InstanceName at module level of Class1 (type PublicInstanceName) to hold the names of each instance. Leave the default name as Class1. Copy and paste the following code into the General section of another module, and then start it with the statement ), ClassNamerand then creates a dialog box in which users can add objects to the collection. To see how this works, choose the Class Module command from the Insert menu and declare a public variable called InstanceName at module level of Class1 (type PublicInstanceName) to hold the names of each instance. Leave the default name as Class1. Copy and paste the following code into the General section of another module, and then start it with the statement ClassNamer in another procedure. (This example only works with host applications that support classes.)

Sub ClassNamer()
    Dim MyClasses As New Collection    ' Create a Collection object.
    Dim Num    ' Counter for individualizing keys.
    Dim Msg As String    ' Variable to hold prompt string.
    Dim TheName, MyObject, NameList    ' Variants to hold information.
    Do
        Dim Inst As New Class1    ' Create a new instance of Class1.
        Num = Num + 1    ' Increment Num, then get a name.
        Msg = "Please enter a name for this object." &; Chr(13) _
         &; "Press Cancel to see names in collection."
        TheName = InputBox(Msg, "Name the Collection Items")
        Inst.InstanceName = TheName    ' Put name in object instance.
        ' If user entered name, add it to the collection.
        If Inst.InstanceName <> "" Then
            ' Add the named object to the collection.
            MyClasses.Add item := Inst, key := CStr(Num)
        End If
        ' Clear the current reference in preparation for next one.
        Set Inst = Nothing
    Loop Until TheName = ""
    For Each MyObject In MyClasses    ' Create list of names.
        NameList = NameList &; MyObject.InstanceName &; Chr(13)
    Next MyObject
    ' Display the list of names in a message box.
    MsgBox NameList, , "Instance Names In MyClasses Collection"

    For Num = 1 To MyClasses.Count    ' Remove name from the collection.
        MyClasses.Remove 1    ' Since collections are reindexed
                ' automatically, remove the first
    Next        ' member on each iteration.
End Sub
© 2018 Microsoft