vba 如何销毁一个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42164719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy an object
提问by Fadi
It seems that Set Object = Nothing
didn't destroy the Fs
Object in this code:
似乎Set Object = Nothing
没有破坏Fs
这段代码中的对象:
Sub Test2()
Dim Fs As New FileSystemObject
Set Fs = Nothing
MsgBox Fs.Drives.Count ' this line works
End Sub
The last line works with no errors!. thats mean Fs
Object is still exists, right?.
最后一行没有错误!。这意味着Fs
对象仍然存在,对吧?。
So how to destroy this Fs
Object.
那么如何销毁这个Fs
Object。
回答by user3598756
it must have to do with the "declare & instantiate" pattern, most often addressed as a "to be avoided" pattern
它必须与“声明和实例化”模式有关,通常被称为“要避免的”模式
if you split them, you get Nothing
after setting it to:
如果将它们拆分,则将其Nothing
设置为:
Sub Test2()
Dim Fs As FileSystemObject
Set Fs = New FileSystemObject
Set Fs = Nothing
MsgBox Fs.Drives.Count ' this line DOESN'T work
End Sub
回答by Mathieu Guindon
Another way to ensure proper destruction of an object, is to yield its object reference to a With
block (i.e. don't declare a local variable):
确保正确销毁对象的另一种方法是将其对象引用交给With
块(即不声明局部变量):
Sub Test()
With New FileSystemObject
MsgBox .Drives.Count
End With
End Sub
The object only ever exists inside the With
block, and when execution reaches the End With
token, if you try it with a custom class module you'll notice that the the class' Class_Terminate
handler runs, effectively confirming the proper destruction of the object.
对象只存在于With
块内,当执行到达End With
令牌时,如果您使用自定义类模块尝试它,您会注意到类的Class_Terminate
处理程序运行,有效地确认了对象的正确销毁。
As for the As New
quirk, as was already explained, if you intend to set the object reference to Nothing
inside that scope, don't declare it with As New
, because VBA willset the object reference to Nothing
, but will also happily ("helpfully") create a new instance for you as soon as you re-reference it again, be it only to verify that the object Is Nothing
.
至于As New
怪癖,如已经解释的,如果你打算将对象引用设置到Nothing
该范围内,不声明它As New
,因为VBA将设置对象引用Nothing
,而且还会高兴地(“有益”)创建一旦您再次重新引用它,就会为您提供新实例,只是为了验证对象Is Nothing
.
Side note, this (annoying) counter-intuitive behavior is specifically what's behind the reasoning for Rubberduck's Object variable is self-assignedcode inspection:
旁注,这种(令人讨厌的)违反直觉的行为是Rubberduck的Object 变量的推理背后的具体原因是自分配代码检查:
(note: if you've got code you want to inspect, know that it runs muchfaster in the actual VBE than on the website)
(注:如果你已经得到了你想要检查的代码,知道它运行多快于实际VBE不是网站上)
(if it wasn't clear already: I'm heavily involved with the Rubberduck project)
(如果还不清楚:我大量参与了 Rubberduck 项目)
回答by Zerk
This amended code seems to work fine. Seems a nuance with the dim/new on the same line. Hopefully someone else can give a better idea as to the reasoning.
这个修改后的代码似乎工作正常。似乎与同一条线上的昏暗/新的细微差别。希望其他人可以对推理提出更好的想法。
As others have commented, if it is a dim within the sub then it will be collected after the sub completes anyway.
正如其他人所评论的那样,如果它在 sub 中是一个暗淡的,那么它无论如何都会在 sub 完成后被收集。
Sub Test2()
Dim Fs As FileSystemObject
Set Fs = New FileSystemObject
Set Fs = Nothing
End Sub