I.T. Discussion Community!
-Collapse +Expand
ASP Classic
Search ASP Classic Group:

Advanced
-Collapse +Expand ASP Classic Store

Prestwood eMagazine

February Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...ASP ClassicOOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
ASP Classic OOP:
ASP Classic Destructor (Class_Terminate)
 
Posted 10 years ago on 1/18/2009
ASP Classic Code Snippet:
 A flashcard from our ASP Classic Flashcards Library
 A code snippet from our ASP Classic Code Snippets Page

KB101830



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: Destructor

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

ASP Classic Destructor

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

Working Example

The following working example contains both a constructor and destructor, ASP's Class_Initialize and Class_Terminate subs. In this example, we are explicitly destroying the object by using Set MyRobot = Nothing. In ASP Classic, it is recommended that you explicitly destroy all objects.

<%@LANGUAGE=VBScript%>
<%Option Explicit%>
<html>
<body>
  
<%
Dim MyRobot
Set MyRobot = new Cyborg
Response.Write "<br>My robot's name is " & MyRobot.CyborgName & "."
  
//Explicitly destroy object.
Set MyRobot = Nothing
%>
  
</body>
</html>
  
<%
Class Cyborg
 Public CyborgName
 
 Public Sub Class_Initialize
  Response.Write "<br>Class created"
  CyborgName = "Cameron"
 End Sub 
  
 Public Sub Class_Terminate
  Response.Write "<br>Class destroyed"
 End Sub
 
End Class

%> 

More Info

Definition:  Class Destructor

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1182A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

1 Advanced Level Question

Question #1: Multiple Choice

In ASP Classic object oriented programming, when an object instance is destroyed, Access VBA calls a special parameter-less sub named what?

Answer:
1. 

Finalize

2. 

Object_Terminate

3. 

Class_Finalize

4. 

Class_Terminate

5. 

Free


 KB Article #101830 Counter
19199
Since 1/18/2009
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,135 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech