0

I have a class:

Public store As String
Public group As String
Public code As Long
Public name As String
Public format As Long
Public minQuantity As Long

And simple function:

Private Function addToCollection(ByVal source As Collection, ByRef dest As Collection)
    Dim pos As position
    For Each pos In source
        dest.Add pos
    Next pos
End Function

Function addToCollection invokes in this piece of code:

Public Function getPositions(ByRef stores As Collection, ByVal groupName As String, ByVal matrixArray As Variant) As Collection
    Dim result As New Collection
    Dim groupCol As Integer, formatCol As Integer
    groupCol = getColByName(groupName)
    formatCol = getColByName(matrix)
    
    Dim filteredByGroupAndFormat As Collection
    Dim item As store
    For Each item In stores
        Set filteredByGroupAndFormat = getPositionsByGroupAndFormat(matrixArray, item.format, formatCol, groupCol, item.name, item.group)
        
        Call addToCollection(filteredByGroupAndFormat, result)
    Next item
    
    Set getPositions = result
End Function

for the first time it works and new positions are adding to the dest collection, but for the second invocation, the collection is not changing and method .Add simply doesn't work. Does anyone familiar with the problem?

I expect that dest collection will populate with new positions each time the function calls

yaroslav96
  • 33
  • 4
  • Your function does not return a value. You are updating dest in addToCollection by reference, but you don't show a function call to addtoCollection so we don't know what you code is doing. Toy should change add to collection to a sub. Please update your code example to show all the class declarations and an example of a call to addtocollection. – freeflow Dec 16 '22 at 11:22
  • No reason to fail with the code you posted. The issue is somewhere else, code you have not posted. – Kostas K. Dec 16 '22 at 11:29
  • @freeflow, I've updated the question to provide more information – yaroslav96 Dec 16 '22 at 11:36
  • "the second invocation" Is that the second call to `getPositions()`' That function is creating a new result each time with `Dim result As New Collection` – CDP1802 Dec 16 '22 at 11:58
  • @CDP1802, the second time invocation is actually second iteration for each loop, inside getPositions() function – yaroslav96 Dec 16 '22 at 12:06
  • Try adding `MsgBox "src=" & source.Count & " dest=" & dest.Count` before and after loop and see what you get. – CDP1802 Dec 16 '22 at 12:17
  • Sorry guys, everything is working, the Debugger shows only the first 256 items. – yaroslav96 Dec 16 '22 at 14:07
  • Does this answer your question? [Collection Maximum Size](https://stackoverflow.com/questions/3136507/collection-maximum-size) – Zephyr Dec 17 '22 at 11:54

0 Answers0