我有一个关于通过数组从标准和自定义类中销毁对象的问题,这是示例:
dim class1 As cClass1
dim class2 As cClass2
dim class3 As cClass3
....
Set class1 = New cClass1
Set class2 = New cClass2
Set class3 = New cClass3
....
使用它们后,我想在最后销毁它们以从内存中释放它们,但我想避免使用
Set class1 = Nothing
Set class2 = Nothing
.... 'and so on
我想用以下方法摧毁它们:
CRLS Array(class1, class2, class3, "and so on")
这里是 sub 可能会这样做:
Private Sub CLRS(ByRef arr As Variant)
Dim i As Integer
For i = 0 To UBound(arr)
If Not arr(i) Is Nothing Then
Set arr(i) = Nothing
Debug.Print "Deleted" 'it will throw "Deleted" but it will not delete element itself
Else
Debug.Print "not deleted" 'just to see status of element
End If
Next
Erase arr
End Sub
但不幸的是,如果我检查“销毁”元素是否真的免费,答案不是,只有选定元素的副本被设置为空。对象像 ByVal 一样传递给数组。