3

I am working with code something like the below. An array of BasketItemViewModel is created in the scope of the Basket class by looping DB results and creating a view model from each row.

When I ditch the Basket class, and Set it to nothing, am I required to loop through the array of BasketItemViewModel and set each of those to nothing too?

Class Basket

    public GuidId
    public BasketItemViewModels
    public TotalItems
    public TotalCost

    Public Property Get TotalCostFormatted()
        TotalCostFormatted = FormatCurrency(TotalCost,0)
    End Property



    public Default function Init(p_GuidId, p_TotalItems, p_TotalCost)
        GuidId = p_GuidId
        BasketItemViewModels = GetBasketItemViewModels()
        TotalItems = p_TotalItems
        TotalCost = p_TotalCost
        set Init = Me
    end function 

    public function GetBasketItemViewModels()
        dim vmArray()

        for each row in dbResults
            // ...get some values...
            set vmArray(i) = (new BasketItemViewModel) (price, quantity, productId)
        next           

        GetBasketItemViewModels = vmArray
    end function 
End Class
user692942
  • 16,398
  • 7
  • 76
  • 175
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64

1 Answers1

5

Nope. It's not necessary. When your Basket object is destroyed, the BasketItemViewModels array goes with it. And when it goes, all of the references held by that array are released.

Consider the following example:

Dim b
Set b = New Basket
Set b = Nothing
WScript.Echo "After"

Class Basket
    Public Eggs
    Sub Class_Initialize()
        Dim a(1)
        Set a(0) = New Egg
        Set a(1) = New Egg
        Eggs = a
    End Sub
End Class

Class Egg
    Sub Class_Terminate()
        WScript.Echo "Egg cracked."
    End Sub
End Class

The output produced by this code is:

Egg cracked.
Egg cracked.
After

proving that the Eggs array is emptied, and its references released, when the Basket object is destroyed.  

user692942
  • 16,398
  • 7
  • 76
  • 175
Bond
  • 16,071
  • 6
  • 30
  • 53