假设我有一个主集合,我想添加其他集合。我有一些可选的函数返回一个集合,我想将它附加到我现有的集合。是否有像VB.NET中的“.AddRange”那样的命令?
VBA代码示例:
Public Sub main()
Dim masterCollection As Collection
Do While addingThings
If addManyThings Then
masterCollection.AddRange functionReturnsCollection
Else
masterCollection.Add aThing
End If
Loop
End Sub
Private Function functionReturnsCollection() As Collection
End Function
答案 0 :(得分:1)
Public Function AppendCollections( _
Optional ByVal FirstCollection As Collection, _
Optional ByVal SecondCollection As Collection, _
Optional ByVal ThirdCollection As Collection _
) As Collection
Dim ReturnCollection As Collection, i As Variant
' Start with an empty Collection
Set ReturnCollection = New Collection
' Append the first Collection
If Not FirstCollection Is Nothing Then
For Each i In FirstCollection
ReturnCollection.Add i
Next i
End If
' Append the second Collection
If Not SecondCollection Is Nothing Then
For Each i In SecondCollection
ReturnCollection.Add i
Next i
End If
'Append the third Collection
If Not ThirdCollection Is Nothing Then
For Each i In ThirdCollection
ReturnCollection.Add i
Next i
End If
' Return the new Collection
Set AppendCollections = ReturnCollection
End Function
答案 1 :(得分:0)
对收藏品不要这么认为。您可以通过Systems Collection(.Net framework)访问您提到的方法,使用ArrayList访问insertRange,例如:
class User < ActiveRecord::Base
end
也许你可以设计一个类来模仿外部。