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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:06:03  来源:igfitidea点击:

How to destroy an object

vbaexcel-vbavb6access-vbaexcel

提问by Fadi

It seems that Set Object = Nothingdidn't destroy the FsObject 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 FsObject is still exists, right?.

最后一行没有错误!。这意味着Fs对象仍然存在,对吧?。

So how to destroy this FsObject.

那么如何销毁这个FsObject。

回答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 Nothingafter 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 Withblock (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 Withblock, and when execution reaches the End Withtoken, if you try it with a custom class module you'll notice that the the class' Class_Terminatehandler runs, effectively confirming the proper destruction of the object.

对象只存在于With块内,当执行到达End With令牌时,如果您使用自定义类模块尝试它,您会注意到类的Class_Terminate处理程序运行,有效地确认了对象的正确销毁。

As for the As Newquirk, as was already explained, if you intend to set the object reference to Nothinginside 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:

旁注,这种(令人讨厌的)违反直觉的行为是RubberduckObject 变量的推理背后的具体原因是自分配代码检查:

RD website's inspection results

RD网站的检查结果

(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