扩展集合类VBA

扩展集合类VBA,vba,collections,Vba,Collections,我创建了一个排序函数,允许自定义对象的实例集合根据其中一个对象属性进行排序。是否可以在VBA中扩展现有的collections类?我不相信VBA支持继承,所以我不知道如何以正确的方式进行。我可以创建一个新模块并将函数放在该模块中,但这似乎不是最好的方法。我将创建一个包装类,公开集合对象的属性,用自己的排序函数替换它。感谢您的响应。我最终创建了自己的类,该类扩展了VBA中的Collections类。如果有人感兴趣,下面是代码 'Custom collections class is based o

我创建了一个排序函数,允许自定义对象的实例集合根据其中一个对象属性进行排序。是否可以在VBA中扩展现有的collections类?我不相信VBA支持继承,所以我不知道如何以正确的方式进行。我可以创建一个新模块并将函数放在该模块中,但这似乎不是最好的方法。

我将创建一个包装类,公开集合对象的属性,用自己的排序函数替换它。

感谢您的响应。我最终创建了自己的类,该类扩展了VBA中的Collections类。如果有人感兴趣,下面是代码

'Custom collections class is based on the Collections class, this class extendes that
'functionallity so that the sort method for a collection of objects is part of
'the class.

'One note on this class is that in order to make this work in VBA, the Attribute method has to be added
'manually.  To do this, create the class, then export it out of the project.  Open in a text editor and
'add this line Attribute Item.VB_UserMemId = 0 under the Item() function and this line
'Attribute NewEnum.VB_UserMemId = -4 under the NewEnum() function.  Save and import back into project.
'This allows the Procedure Attribute to be recognized.

Option Explicit

Private pCollection As Collection

Private Sub Class_Initialize()
    Set pCollection = New Collection
End Sub

Private Sub Class_Terminate()
    Set pCollection = Nothing
End Sub

Function NewEnum() As IUnknown
    Set NewEnum = pCollection.[_NewEnum]
End Function

Public Function Count() As Long
    Count = pCollection.Count
End Function

Public Function item(key As Variant) As clsCustomCollection
    item = pCollection(key)
End Function

'Implements a selection sort algorithm, could likely be improved, but meets the current need.
Public Sub SortByProperty(sortPropertyName As String, sortAscending As Boolean)

    Dim item As Object
    Dim i As Long
    Dim j As Long
    Dim minIndex As Long
    Dim minValue As Variant
    Dim testValue As Variant
    Dim swapValues As Boolean

    Dim sKey As String

    For i = 1 To pCollection.Count - 1
        Set item = pCollection(i)
        minValue = CallByName(item, sortPropertyName, VbGet)
        minIndex = i

        For j = i + 1 To pCollection.Count
            Set item = pCollection(j)
            testValue = CallByName(item, sortPropertyName, VbGet)

            If (sortAscending) Then
                swapValues = (testValue < minValue)
            Else
                swapValues = (testValue > minValue)
            End If

            If (swapValues) Then
                minValue = testValue
                minIndex = j
            End If

            Set item = Nothing
        Next j

        If (minIndex <> i) Then
            Set item = pCollection(minIndex)

            pCollection.Remove minIndex
            pCollection.Add item, , i

            Set item = Nothing
        End If

        Set item = Nothing
    Next i

End Sub

Public Sub Add(value As Variant, key As Variant)
    pCollection.Add value, key
End Sub

Public Sub Remove(key As Variant)
    pCollection.Remove key
End Sub

Public Sub Clear()
    Set m_PrivateCollection = New Collection
End Sub
“自定义集合类基于集合类,该类扩展了
'功能性,因此对象集合的排序方法是
”他说。
'关于这个类的一个注意事项是,为了在VBA中工作,必须添加属性方法
“手动。为此,创建类,然后将其从项目中导出。在文本编辑器中打开,然后
'在Item()函数和此行下添加此行属性Item.VB_UserMemId=0
'Attribute NewEnum.VB_UserMemId=-4位于NewEnum()函数下。保存并导入到项目中。
'这允许识别过程属性。
选项显式
私人pCollection作为集合
私有子类_Initialize()
Set pCollection=新集合
端接头
私有子类_Terminate()
设置pCollection=Nothing
端接头
函数NewEnum()作为IUnknown
Set NewEnum=pCollection。[[u NewEnum]
端函数
公共函数Count()的长度为
Count=pCollection.Count
端函数
公共功能项(键作为变体)作为clsCustomCollection
项目=pCollection(键)
端函数
'实现了一种选择排序算法,可能会有所改进,但满足当前需要。
公共子SortByProperty(sortPropertyName为字符串,SortScending为布尔值)
将项目变暗为对象
我想我会坚持多久
Dim j尽可能长
暗淡的minIndex如长
将最小值作为变量
Dim testValue作为变量
将SwapValue设置为布尔值
像线一样模糊
对于pCollection.Count-1的i=1
集合项目=P集合(i)
minValue=CallByName(项、sortPropertyName、VbGet)
minIndex=i
对于j=i+1到pCollection.Count
集合项目=P集合(j)
testValue=CallByName(项、sortPropertyName、VbGet)
如果(排序)那么
SwapValue=(testValueminValue)
如果结束
如果(swapvalue)那么
minValue=testValue
minIndex=j
如果结束
设置项=无
下一个j
如果(minIndex i)那么
设置项=pCollection(minIndex)
pCollection。删除minIndex
pCollection.additem,i
设置项=无
如果结束
设置项=无
接下来我
端接头
公共子添加(值作为变量,键作为变量)
pCollection.addvalue,key
端接头
公共子删除(密钥作为变体)
pCollection。删除密钥
端接头
公共分区清除()
设置m_PrivateCollection=新集合
端接头

一个流行的选项是使用作为一种超能力的集合/字典对象,它内置了对的支持。虽然您使用的是ADO,但您可以。

+1这是一个非常有趣的想法,当我有时间玩它的时候,我会给它一个机会。注意,你需要在文本编辑器中打开类模块,并在函数NewEnum行之后添加
属性NewEnum.VB_UserMemID=-4
,以获得每个语法的
。整洁的代码。请确保将属性IndexOf添加到您的集合中-@Rubberduck已经显示了这一点