Y
Yakov
Перебор коллекции документов - типичная задача.
У меня обычно это выглядит примерно так:
Очень много кода, не правда ли? Вот бы сократить его до такого:
Сказано - сделано!
У меня обычно это выглядит примерно так:
Код:
Dim collection As NotesDocumentCollection
Dim document As NotesDocument
Set collection = database.Search(...)
If collection Is Nothing Then Exit Sub 'на всякий случай
If collection.Count = 0 Then Exit Sub
Set document = collection.GetFirstDocument()
While Not document Is Nothing
Call processDocument(document)
Set document = collection.GetNextDocument(document)
Wend
Код:
Dim iterator As CollectionIterator
Set iterator = New CollectionIterator(database.Search(...))
While iterator.hasNext()
Call processDocument(iterator.next())
Wend
Код:
Public Class CollectionIterator
Private collection As Variant
Private cursor As Variant
Public Sub new(collection As Variant)
Set Me.collection = Nothing
Set cursor = Nothing
If collection Is Nothing Then Exit Sub
If collection.count = 0 Then Exit Sub
Set Me.collection = collection
Set cursor = collection.getFirstDocument()
End Sub
Public Function hasNext() As Boolean
hasNext = Not cursor Is Nothing
End Function
Public Function next() As Variant
Dim tmp As Variant
If Not hasNext() Then
Error 188, "Collection has no more elements." 'ErrIllegalUseOfFunction
End If
Set tmp = cursor
Set cursor = collection.getNextDocument(cursor)
Set Me.next = tmp
End Function
End Class