Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesASP Classic  Print This     

Destructor (ASP Classic and VB.Net Cross Reference Guide)

By Mike Prestwood

ASP Classic versus VB.Net: A side by side comparison between ASP Classic and VB.Net.

OOP Details

More object oriented (OO) stuff.


[Other Languages] 

General Info: Class Destructor

A special class method called when an object instance of a class is destroyed. With some languages they are called when the object instance goes out of scope, with some languages you specifically have to call the destructor in code to destroy the object, and others use a garbage collector to dispose of object instances at specific times.

Desctructors are commonly used to free the object instance but with languages that have a garbage collector object instances are disposed of when appropriate. Either way, destructors or their equivalent are commonly used to free up resources allocated in the class constructor.

Languages Focus

Are object instances freed with a garbage collector? Or, do you have to destroy object instances.

ASP Classic:   Class_Terminate

When an object instance is destroyed, ASP calls a special parameter-less sub named Class_Terminate. For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.

To explicitly destroy an object, use Set YourClass = nothing. If the Class object is explicitly destroyed, the client returns with the script engine error details.

When an object instance is created from a class, ASP calls a special sub called Class_Initialize.

Syntax Example:
Class Cyborg
  Public Sub Class_Terminate
    Response.Write "<br>Class destroyed"
End Sub
End Class
VB.Net:  "Finalizer" Finalize()

In VB.Net you cannot explicitly destroy a managed�object. Instead, the .Net Framework's garbage collector (GC) takes care of destroying all objects. The GC destroys the objects only when necessary. Some situations of necessity are when memory is exhausted or you explicitly call the System.GC.Collect() method. In general, you never need to call System.GC.Collect().

In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In VB.Net, a finalizer is an overridden sub called Finalize. Because you don't know when the garbage collector will call your finalizer, Microsoft recommends you implement the IDisposable interface for non-managed resources and call it's Dispose() method at the appropriate time.

Syntax Example:
Class Cyborg
� Protected Overrides Sub Finalize()
��� 'Free non-managed resources here.
��� MyBase.Finalize()
� End Sub
End Class

Sales Website: Or visit our legacy sales site:

©1995-2024 Prestwood IT Solutions.   [Security & Privacy]