我已经创建了一个排序函数来允许基于其中一个对象属性对自定义对象的实例进行排序。是否可以扩展VBA中的现有集合类?我不相信VBA支持继承,所以我不知道如何以适当的方式去解决这个问题。我可以创建一个新模块并将该函数放置在该模块中,但这似乎不是实现该模块的最佳方式。Extend Collections Class VBA
3
A
回答
5
感谢您的回复。我最终创建了自己的类,它扩展了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
0
我会创建一个包装类,公开对象的属性,用你自己的替换sort函数。
2
一种流行的选择是使用一个ADO disconnected recordset作为一种hyperpowered收集/字典对象,它有内置的支持Sort的。虽然您正在使用ADO,但您仍可使用don't need a database。
+0
+1这是一个非常有趣的想法,当我有时间玩这个游戏的时候,我会给你一个镜头。 – 2009-11-19 14:29:16
相关问题
- 1. Extend DataSet Class Not Working
- 2. Extend Class second time sencha touch 2
- 3. Dojo:有什么关于'class extend'的吗?
- 4. @extend Spine.Model.Ajax.Methods
- 5. Outlet Collections
- 6. Collections -ArrayList
- 7. class property not working excel vba
- 8. querydsl-collections and groupBy
- 9. Extend Parse.User
- 10. Extend System.Windows.Forms.ComboBox
- 11. @extend类
- 12. Extend Galleriffic buildImage
- 13. Extend Hash类
- 14. Extend Membership API,MembershipUser
- 15. Symfony2 Forms和Polymorphic collections
- 16. Extend Request.IsSecureConnection属性
- 17. Extend Doctrine EntityRepository
- 18. Android customListView extend BaseAdapter
- 19. MultiScaleTileSource和Collections
- 20. Collections排序Java
- 21. JavaScript Collections API?
- 22. @XmlElementWrapper for unwrapped collections
- 23. Canont Extend Zizaco \ Confide \ ConfideUser
- 24. Clojure extend Assotiative arity
- 25. Extend C#List.Last
- 26. jQuery插件$ .extend
- 27. Angular extend vs $ provide.decorator
- 28. Extend Workflow Foundation FlowChart
- 29. Backbone.js:`extend` undefined?
- 30. c#collections列表类
只需要注意,您需要在文本编辑器中打开类模块,并在'Function NewEnum'行后面添加'Attribute NewEnum.VB_UserMemID = -4',以使'For Each'语法正常工作。 – RubberDuck 2014-05-30 19:27:53