Understanding Objects, Methods, and Properties

Programming languages are developed to solve certain problems or enable certain functionalitiesthey don't just pop up from nowhere. For example, Flash ActionScript was created to enable developers to get the most out of native Flash capabilities and features. For this reason, several features of ActionScript are unique to Flash, and these features distinguish ActionScript from languages similar to it, such as JavaScript.

ASP and ColdFusion are no different. Each was designed to enable dynamic Web site functionality within the context of the HTTP protocol. Their respective developers knew that you would want to do certain tasks, such as send data between pages, connect to databases, and generate email messages. To simplify these tasks, each language includes built-in objects. Objects are generic entities created for the purpose of simplifying a given task. For example, ASP's NewMail object makes it easy to create and send new mail messages from within ASP.

NOTE

Strictly speaking, the NewMail object doesn't belong to ASP, but rather to a larger class of objects built into Windows NT, 2000, and XP, and available to IIS. This nuance has little consequence for our purposes, and I only mention it here for accuracy.


To use an object, you must first create an instance of it. If you've ever used a Library in Macromedia Flash, Fireworks, or Dreamweaver, you are already familiar with the relationship. The object, or more properly, class, exists in potential only; you can't directly use the class itself. When you make an instance of it, you make a unique copy of that class, but you also customize it for its surroundings. You might think of it as the difference between the scientific definition of homo sapiens on the one hand, and an individual person on the other. All members of homo sapiens have height, hair color, and weight attributes. But these values differ for each person.

Most object classes have built-in features. Generally, these fit into two categories: properties and methods. Some objects even have child objects with their own properties and methods. Properties are descriptive attributes. To use the homo sapiens example, height, hair color, birthday, present location, and weight are all properties. Methods are what the object can do. Humans can walk, dance, sing, and sleep; each of these would be a method of the homo sapiens. When you create an instance of an object, you often define its properties. When you want something to happen, you call one or more of its methods.

Finally, when you create an object instance, you usually need to give it a unique ID, or name. This name enables the script to keep track of it, since you can usually use multiple instances of the same object in the same script or document. Once again, humans have names for the same reasonnames help us identify and refer to each other.

To summarize, you can achieve specific kinds of functionality in scripts by using built-in objects. To use an object, you must create an instance of it. To enable the script to properly identify your instance, you give it a unique name. Finally, to make use of the instance (and accomplish your task), you set its properties and call its methods.

In the steps that follow, you will write a script in which you can see each of these steps in action.

A final note before continuing: One of the reasons that ColdFusion is comparatively easy to learn and use is that it hides the complexity of object-oriented programming behind its tags. Just by using certain tags and filling out their parameters, you provide the ColdFusion server with enough information to quietly instantiate and activate objects in the background. If you look at the ASP and ColdFusion code side-by-side in the following steps (and you should be doing so as a part of using this book!), you will see that both require the exact same customizing information, but that ColdFusion is simpler to deploy.

The following steps are separated to reflect the different stages of using an object.

NOTE

These steps continue from the last task, "Writing the Code to Send a Message."


  1. Using the code view, position the cursor at the beginning of line 3 and press Enter/Return several times to make room for a new script. ASP users only, type the following code:

    <!  --METADATA TYPE="TypeLibrary"
    FILE="CDONTS.DLL"
    VERSION="1.2"
    -->
    

    This code block makes the library that contains the NewMail object available to the ASP page. As mentioned earlier, the NewMail object is not part of ASP proper, but rather a collection of objects called Collaboration Data Objects for Windows NT Server (or CDONTS for short). To make this object available to ASP on this page, you need to tell ASP to load the objects in the CDONTS collection.

    This block of code is unnecessary in ColdFusion, because the mail object is already built-in and available.

  2. To instantiate and give identity to a new mail object, enter the following code:

    For ASP:

    <%
    Dim objMessage
    Set objMessage = Server.CreateObject("CDONTS.NewMail")
    %>
    

    For ColdFusion:

    <cfmail>
    </cfmail>
    

    The ColdFusion code is fairly self-explanatory: These two tags tell ColdFusion to create a new mail object. It still needs help before it's useful, but at least you've created it. Behind the scenes, ColdFusion also gives this mail object a unique ID, so in this simple step you have accomplished several tasks.

    The ASP code is (not surprisingly) somewhat more cryptic. The <% and %> indicate that everything in between is ASP code. The second line, Dim objMessage, creates a new variable. This is an arbitrary nameI called it objMessage just to be descriptive, using the obj prefix so I remember that it contains (or will contain) an object. The third line creates a new object of the NewMail class, stored in the CDONTS collection. It also sets the value of objMessage to the newly created object, effectively giving the NewMail instance a unique ID: objMessage.

    graphics/06fig08.gif

  3. Customize the instance so that it can send desired information to the proper email address. To do so, add the following code just above the closing %> (ASP) or </cfmail> (ColdFusion):

    For ASP, enter the following code (all on one line), substituting [enter your email address] with your email address:

    [View full width]
    objMessage.Send "[enter your email address]","[enter your email address]","This is the graphics/ccc.gifMessage Subject","This is the message body"

    Then, ASP users should add a new line, as follows:

    Set objMessage = Nothing
    

    For ColdFusion, amend the <cfmail> tag so that it reads as follows (all on one line), substituting [enter your email address] with your actual email address:

    [View full width]
    <cfmail from="[enter your email address]" to="[enter your email address]" subject="This graphics/ccc.gifis the Message Subject">

    ColdFusion users should then add a new line between the opening and closing <cfmail> tags and type:

    This is the message body.
    

    Both ASP and ColdFusion require you to specify who the message is from, to whom it is being sent, the subject, and the body text itself.

    • ColdFusion uses easy-to-read attribute="value" syntax. The body of the message appears between the <cfmail> tags (as opposed to inside the opening <cfmail> tag, like the other attributes). The action of the messagethe fact that you want to send itis implied in ColdFusion.

    • For ASP, you call the Send method, which means that you are explicitly telling ASP to send the message. The Send method has a number of parameters. These must be listed in the proper order right after the Send method is called, as follows: From, To, Subject, and Body. The next line sets the value of objMessage to Nothing, which releases the NewMail object. In doing so, you make it possible for the page to send a different message using the same object in the future.

    The completed script for ASP looks as follows:

    [View full width]
    <! --METADATA TYPE="TypeLibrary" FILE="CDONTS.DLL" VERSION="1.2" --> <% Dim objMessage Set objMessage = Server.CreateObject("CDONTS.NewMail") objMessage.Send "[enter your email address]","[enter your email address]", "This is the graphics/ccc.gifMessage Subject","This is the message body" Set objMessage = Nothing %>

    The completed script for ColdFusion looks as follows:

    [View full width]
    <cfmail from="[enter your email address]" to="[enter your email address]" subject="This graphics/ccc.gifis the Message Subject"> This is the message body. </cfmail>

    graphics/06inf01.gif

  4. Save and Put the file on your remote server. Click anywhere in the document and press F12 to test it.

    When your browser opens, you should see the message indicating that the message has been sent. If you see an error message, and you've double-checked the spelling in your code, there is something wrong with your server's mail configuration. If you are using a staging or production server with your work or ISP, you can contact the server administrator for troubleshooting. If you are working on a standalone machine running IIS, you can try to troubleshoot on your own. In this instance, troubleshooting this issue may be more trouble than it's worth. The point is that you should understand the code and know that it will work on a production server, provided that it is configured to allow email to be sent. But dropping everything to spend potentially several frustrating hours debugging your server is not the most effective way to learn dynamic Web development; you might just want to move on.

    Now for the acid test: Check your email. You should have a received a message, like the one shown in the screenshot, except with your own email address. Needless to say, this is not the most exciting email message you've ever received, and since it's hard-coded to contain that text, it's not likely to improve.

    graphics/06fig09.gif

    If you did not receive the message, then one of several things could have gone wrong. By far the most likely is that your server is not properly configured to send SMTP email messages. Users with IIS might check Control Panel > Administrative Tools > Services and make sure that Simple Mail Transport Protocol is listed as Started. You can also look in Control Panel > Administrative Tools > Internet Information Services (XP) or Internet Services Manager (Windows 2000), select the computer with the server, and explore the Default SMTP Virtual Server's properties.

    If you are using ColdFusion, most likely you did not enter the correct Internet or IP address of your outgoing SMTP mail server.

  5. Close messageSent.asp.

    Now that you have the messaging itself working, you need to make it useful, by putting meaningful content into the message. To do that, you'll use a form to collect the message data from the user, and then you'll send that data to the mail object, which in turn will send it (ostensibly) to Newland Tours staff.



     
    ASPTreeView.com
     
    Evaluation has µЖИЅЛД·КУМГexpired.
    Info...