About Me

Friday, 20 April 2012

ASP Object

ASP Object

Objects are a way of encapsulating multiple methods (they're like functions) and variables in one easy to manage Uber-Variable (an Object). Objects in ASP resemble other Object Oriented Programming languages. In this lesson we will be using the ASP CDO.Message object as our example object to be dissected.

ASP Object Overview

Objects were created to combat the increasing complexity of programming. The rationale for understanding and using Objects in your programming is to make programming easier and your code more human readable.

ASP Create an Object - Server.CreateObject

An object in ASP is created by passing a name string to the Server.CreateObject function(actually referred to as a method). The string to create a Message object is "CDO.Message". We will be creating a CDO.Message object in this example.
Note: Because objects are special there is a special way that you create and destroy them using the Set keyword. These areas are marked in red in the example below.

ASP Code:

<%
Dim myObject
Set myObject = Server.CreateObject("CDO.Message")

'You must Set your objects to "nothing" to free up the
'the computer memory that was allocated to it
Set myObject = nothing
%>
That wasn't too painful, was it? Let's cover some more bases on the object model.
Objects are a collection of related things that are combined into this blob of programming goo that can be created and destroyed whenever we may need it. For example say that you wanted to make an object that allowed you to send an email...
Well there are certain things all emails have: To, From, CC, Subject, etc. This list of variables that are common to every email would be quite tiresome to have to create for every email we sent. Wouldn't it be nice if we could create some sort of Uber-Variable(Object) that would group all these smaller variables into one thing?

ASP Object Properties

These smaller variables are commonly referred to as an object's properties and the format for setting these properties is nearly identical to setting a variable equal to a value.
The correct syntax for setting an object's properties is:
  • objectName.propertyName = someValue
In this tiny example below we are creating a new mail object and setting its To and From properties.

ASP Code:

<%
Dim myObject
Set myObject = Server.CreateObject("CDO.Message")

'Then we set the To and From properties
myObject.To = "little.timmy@example.com"
myObject.From = "huge.jill@example.com"

'You must Set your objects to "nothing" to free up the
'the computer memory that was allocated to it
Set myObject = nothing
%>
Now I know we didn't DO anything in the above example, but we still need to learn a bit more about objects before we can get anything done! Objects, besides having a clump of associated common variables, may also have a collection of functions(which become referred to as methods) associated with them.
These methods are processes that you would want to commonly do to either manipulate the variables of the object or to use the variables to do something. In our Message object we have a collection of information that, when put together into the proper email form and sent to an email service will become an email.
All this complex code has been programmed by Microsoft employees and stored into the Message objects Send method.

ASP Object Methods

We cannot see the code that was used to program the Send method, but that's one of the great things about using object programming. You know what you need to know and nothing more. In our example below we create a Message object and set the necessary properties and send it off with the Send method.

ASP Code:

<%
Dim myObject
Set myObject = Server.CreateObject("CDO.Message")

'Then we set the To and From properties
myObject.To = "little.timmy@example.com"
myObject.From = "huge.jill@example.com"
myObject.Subject = "Can you see me?"
myObject.TextBody = "I'm really really big!"

myObject.Send()

'You must Set your objects to "nothing" to free up the
'the computer memory that was allocated to it
Set myObject = nothing
%>
Note:If you are running this on your home computer there are a slough of issues that may arise with sending an email. Microsoft has a large FAQ about using the CDO.Message object that may help you. Knowing Microsoft, that link may go dead soon.

ASP Object Summary

In this lesson you learned how to create and destroy an object in ASP. You also learned how to access the properties and utilize the methods of an object. If you still have no idea what this lesson was about, hopefully there's enough information for you to hack out what you need to do in your ASP project.
However, there are many good references to object oriented programming and here are a few to help out those that would like a crash course: Sun's Take on Objects, Crazy Germans Know Their Stuff!, and Wikipedia.

0 comments:

Post a Comment