This question is about scope and lifetime of vba objects

I am unclear as to where I must instantiate collections and their objects


for an example

assume I have a Main program
within it i generate some values
and pass them off to a subroutine to produce data.

Somehow I wish to put the output of the subroutine
into objects which in turn are added to a collection

Can I instantiate the collection and its collected objects within the
subroutine and pass them out?

In the language C, that is a poor approach because of data lifetime issues
and memory management (usually one wishes to create and kill memory
in the same code module so that memory "leaks" (unused memory is not
released and hence one gradually runs out of memory to allocate) do not
occur.

What happens in VBA?

I could instantiate an empty collection and pass it byref to the subroutine
wherein inside of the subroutine I create the objects that will be added to
the collection. But I am worried about the lifetime of those objects added to
the collection when the subroutine passes focus back to the main program.

Please note that I can't instantiate the objects that will be added to the
collection before the subroutine does its calculations as I can not predict
how many objects I will need to create

I am sure multiple solutions exist to solve my problem. In C one might use
global variables. I would like to avoid such a concept if possible.

thank you