Board index » Visual Studio » Class_Terminate emulation

Class_Terminate emulation

Visual Studio369
Is there anyway to force the equivalent to VB 6's Class_Terminate event when

a class object goes out of scope. I have several classes in VB 6 that

depend on the Class_Terminate event firing as soon as the class variable

goes out of scope and need to be able to duplicate this functionality in VB

2005.



Thanks,

Mike Ober.


-
 

Re:Class_Terminate emulation

In article <egBVhcNUFHA.1148@tk2msftngp13.phx.gbl>, Michael D. Ober wrote:

Quote
Is there anyway to force the equivalent to VB 6's Class_Terminate event when

a class object goes out of scope. I have several classes in VB 6 that

depend on the Class_Terminate event firing as soon as the class variable

goes out of scope and need to be able to duplicate this functionality in VB

2005.



Thanks,

Mike Ober.





The closest you can can get is to implement Dispose. Here is a

reference to the general pattern:



http://msdn.microsoft.com/library/default.asp?url" rel="nofollow" target="_blank">msdn.microsoft.com/library/default.asp=/library/en-us/cpgenref/html/cpconFinalizeDispose.asp



Once you have that, in VB.NET 2005 you can use the using statement (like

c#) to sort of fake deterministic finalization...



Using theVariable As New TheClass ()

'Do Stuff with theVariable

End Using



Dispose will automatically be called when you hit the end of the using

block - even if it is due to an exception. In versions previous to

2005, you would have to do something like:



Dim myVariable As New TheClass

Try

' do stuf with myVariable

Finally

myVariable.Dispose ()

End Try



HTH

--

Tom Shelton [MVP]

-