class 包含一个对象数组。我是否需要在终止包含 class 时将它们中的每一个都设置为空?

A class contains an array of objects. Do I need to set each of them to nothing on terminating the containing class?

我正在使用类似下面的代码。通过循环数据库结果并从每一行创建视图模型,在 Basket class 的范围内创建了一个 BasketItemViewModel 的数组。

当我放弃 Basket class 并将其设置为空时,我是否需要遍历 BasketItemViewModel 数组并将其中的每一个设置为空?

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

没有。这不是必需的。当您的 Basket 对象被销毁时,BasketItemViewModels 数组也会随之销毁。当它消失时,该数组持有的所有引用都会被释放。

考虑以下示例:

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

这段代码产生的输出是:

Egg cracked.
Egg cracked.
After

证明当 Basket 对象被销毁时,Eggs 数组被清空,其引用被释放。