Excel VBA 对象构造函数和析构函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/571327/
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
Excel VBA object constructor and destructor
提问by notnot
I need to make some custom objects in VBA that will need to reference each other and I have a some issues.
我需要在 VBA 中创建一些需要相互引用的自定义对象,但我有一些问题。
First - how do object constructors work in VBA? Are there constructors?
首先 - 对象构造函数如何在 VBA 中工作?有构造函数吗?
Second - are there destructors? How does VBA handle the end of the object lifecycle? If I have an object that references others (and this is their only reference), then can I set it to Nothing and be done with it or could that produce memory leaks?
第二 - 有析构函数吗?VBA 如何处理对象生命周期的结束?如果我有一个引用其他对象的对象(这是他们唯一的引用),那么我可以将它设置为 Nothing 并用它完成还是会产生内存泄漏?
This quasi-OO stuff is just a little bit irritating.
这种准面向对象的东西只是有点刺激。
采纳答案by Will Rickards
VBA supports Class Modules. They have a Class_Initialize event that is the constructor and a Class_Terminate that is the destructor. You can define properties and methods. I believe VBA uses reference counting for object lifecycle. Which is why you see a lot of Set whatever = Nothing in that type of code. In your example case I think it will not leak any memory. But you need to be careful of circular references.
VBA 支持类模块。它们有一个作为构造函数的 Class_Initialize 事件和一个作为析构函数的 Class_Terminate 事件。您可以定义属性和方法。我相信 VBA 在对象生命周期中使用引用计数。这就是为什么你会在那种类型的代码中看到很多 Set nothing = Nothing。在您的示例中,我认为它不会泄漏任何内存。但是你需要小心循环引用。
回答by Colin
If you are making a class module in VBA, for the constructor, you can use:
如果您在 VBA 中创建类模块,对于构造函数,您可以使用:
Private Sub class_initialize()
....
End Sub
There are no destructors, since VBA is garbage collected. Just make sure to clean up any circular references, and you should avoid any possible memory leaks.
没有析构函数,因为 VBA 是垃圾收集的。只要确保清理任何循环引用,您就应该避免任何可能的内存泄漏。
回答by greg
It's been a while since I've used them, but I don't think you can pass parameters into the constructors. I think that was one of the problems I ran into, but I was running into so many issues of how thse classes worked and how I expected them to work that I may be misremembering.
我已经有一段时间没有使用它们了,但我认为您不能将参数传递给构造函数。我认为这是我遇到的问题之一,但是我遇到了很多关于这些类的工作方式以及我期望它们如何工作的问题,我可能记错了。
回答by TechnoS
There exists Class_Terminate which is pretty much the same as destructor.
存在 Class_Terminate 与析构函数几乎相同。
回答by Elloco
I confirme that class_initialize and class_terminate.
我确认 class_initialize 和 class_terminate。
You can check it by writting this TestClass:
您可以通过编写此 TestClass 来检查它:
Public testVar As Integer
Private Sub class_initialize()
    Debug.Print "Class init"
    testVar = 10
End Sub
Private Sub class_terminate()
    Debug.Print "Class terminate"
End Sub
And write this code in a module :
并在模块中编写此代码:
Sub test()
   Dim myTestClass As New TestClass
   Debug.Print myTestClass.testVar
End Sub
And then you will see logs in Debug window. But with this test, we can see that class_initialize isn't called when you create the instance (with new) but only at the first call of a methode in the instance or getting a var value.
然后您将在调试窗口中看到日志。但是通过这个测试,我们可以看到 class_initialize 在您创建实例(使用 new)时没有被调用,而是在实例中第一次调用方法或获取 var 值时调用。
The class_terminate seems to be called at the end of the testModule (If an instance is maked on Thisworkbook object, terminate is "never" called... probably only when the workbook is closed or when Excel is closed)
class_terminate 似乎在 testModule 的末尾被调用(如果在 Thisworkbook 对象上创建了一个实例,则“从不”调用终止......可能仅当工作簿关闭或 Excel 关闭时)

