Products
Services
Contact Us

Scripts: Asp :: Asp classes :: Library Article #13

Developer's Section

Creating and Using Classes in ASP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3361)
Favorites:

Learn how to create and use a simple class in ASP.

It is very simple to create a class in ASP. Some of the required sintax is very similar to any object oriented programming language that is out there. For the most part adding a constructor is simple as well as the destructor method (which is executed when the class is terminated.

Example:

In a file named library_book_class.asp we are going to create a class the following way:

 Code Snippet 1


<%

Class libraryBook

  dim title, author, price, credits, dateObjectCreated 
  public ISBN
  private internalCode


  public Property Let title(value)

    title = value

  End Property

  public Property Let author(value)

    author = value

  End Property

  public Property Let price(value)

    price = value

  End Property

  public Property Let credits(value)

    credits = value

  End Property

  public Property Let ISBN(value)

    ISBN = value

  End Property

  public Property Let internalCode(value)

    internalCode = value

  End Property

  'Declaring the constructor
  Private Sub Class_Initialize()

    dateObjectCreated = Now

  End Sub

  'Declaring a function
  Public Function getBookDescription()

    Dim thisDescription : thisDescription = "Description Requested on: " & Now

    thisDescription = thisDescription & "Book Title: " & title & "<br>"
    thisDescription = thisDescription & "Book ISBN: " & ISBN & "<br>"
    thisDescription = thisDescription & "Book Author: " & author & "<br>"
    thisDescription = thisDescription & "Book Price: " & price & "<br>"
    thisDescription = thisDescription & "Book Credits: " & credits & "<br>"

    getBookDescription = thisDescription

  End Function

  'Declaring the destructor
  Private Sub Class_Terminate()

    dateObjectCreated = ""

  End Sub


End Class

%>


Now in a file named test_class.asp we are going to test the library book class the following way:

 Code Snippet 2


<!--#include file ="library_book_class.asp"-->

<%

Dim myBook

'Declare a class
Set myBook = new libraryBook

myBook.title   = "Back to the Future"
myBook.author  = "John Doe"
myBook.price   = "$35.67"
myBook.credits = "Editorial 1"
myBook.ISBN    = "AA-Y-45897738"
myBook.internalCode = "7-de-mp-456"


Response.Write myBook.getBookDescription()


%>




See other Scripts in Asp classes

Submit Your Scripts:

If you would like to have your ASP & ASP.NET scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]