我正在使用类似下面的代码。通过循环 DB 结果并从每一行创建 View 模型,在 Basket
类的范围内创建了一个 BasketItemViewModel
数组。
当我放弃 Basket
类并将其设置为空时,我是否需要遍历 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
数组被清空,并且它的引用被释放。
关于arrays - 一个类包含一个对象数组。我是否需要在终止包含类时将它们中的每一个都设置为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214401/