扩展集合VBA [英] Extend Collections Class VBA

查看:144
本文介绍了扩展集合VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a sort function to allow a collection of instances of a custom object to be sorted based on one of the objects properties. Is it possible to extend the existing collections class in VBA? I do not believe inheritance is supported in VBA, so I am not sure how to go about this in the proper way. I could just create a new module and place the function in that module, but that doesn't seem like the best way of doing it.

推荐答案

感谢您的回应。我最终创建了自己的类,扩展了VBA中的Collections类。下面是任何人感兴趣的代码。

Thanks for the responses. I ended up creating my own class which extends the Collections class in VBA. Below is the code if anyone is interested.

'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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆