Reputation: 573
I have created a C# library's COM object in VBA code (Excel). This C# library logs all messages in the log file. I have written some code in finally method and wish to invoke it each time I close the object through excel. But whenever I close the object through excel it does not call the code available in finally method. Please suggest how should I forcibly invoke the finally code through excel.
Upvotes: 0
Views: 122
Reputation: 6780
You actually want your "finally" code to go in the Class Destructor. Once this code is written, it will execute when there are no instances of the class left. VBA (thus, VB6) doesn't have a garbage collector per se, but it does use reference counting to automatically clean up after itself. When there are 0 references to an object in memory that object's destructor will be executed and the object destroyed.
How would this work? When you create an instance of the Object that is 1 reference: Set MyClass = New SomeClass
when that variable is set back to nothing the reference count is decremented back to 0. This can happen two ways:
Set MyClass = Nothing
Hope that helps!
Upvotes: 1