BEGINNING ACTIVE SERVER PAGES 3.0 PART 3 – ALTERING THE PROPERTIES OF AN OBJECT

A program that uses the instance can then retrieve the values associated to these properties. Alternatively, they can be used by a method or event to perform some action. The programmer working with the instance of the telephone object is responsible for setting the values of many properties; other properties will be set based on the results of methods being called.

Setting a Property

First, let’s look at how to set a property. The four properties that we’ll use here to describe our instance of the telephone object are:

  • Color
  • Material
  • Weight
  • NumberOfKeys

When the instance of our object is created, these values are left blank or set to default values. It is up to the program that creates the object to set the specific values that we want.

Try It Out – Setting Property Values

In this example, we will be configuring the properties of our object so that it represents this telephone:

unnamed-file-44 BEGINNING ACTIVE SERVER PAGES 3.0 PART 3 - ALTERING THE PROPERTIES OF AN OBJECT

1. Using your editor of choice, enter the following source code:
<%
Option Explicit
Dim objTelephone
Set objTelephone = Server.CreateObject(“MyTelephone.Telephone”)

objTelephone.Color = “Blue”
objTelephone.Material = “Thermoplastic”
objTelephone.Weight = 22
objTelephone.NumberOfKeys = 12

Response.Write “Done”
Set objTelephone = Nothing
%>

2. Save this file, with the name SetProperties.asp, to your BegASP directory.

3. View the file in your web browser. If everything worked properly, then the browser will display the word Done.

unnamed-file-268 BEGINNING ACTIVE SERVER PAGES 3.0 PART 3 - ALTERING THE PROPERTIES OF AN OBJECT

How It Works

The first step in obtaining a reference to an object is to allocate a variable to hold the reference. The variable is created using the Dim statement:

 Dim objTelephone

You’ll recall from Chapter 4 that the variables in VBScript are in fact variants.

The next step actually creates the object:

 Set objTelephone = Server.CreateObject("MyTelephone.Telephone")

This is done using the Server.CreateObject method. This is the process of instantiation, that we referred to earlier in the chapter, we discussed the concept of instances. The Server object is one of the built-in ASP objects; objects are everywhere; here we are using an object to create an object. This method has one parameter – the name of the object you want to create. The method also has a return value – it’s a reference to an instance of the object.

Since the value returned by the CreateObject method is a reference to an instance of the Telephone object, we must use the Set statement to assign its reference to our variable. The Set statement is a VBScript statement that lets us store object references in variables. Since the return value is a reference to the object, we have to use the Set method to store its value for later use. We will cover the CreateObject method in more detail in Chapter 11.

Now that we have our reference to the instance of the telephone object, we can go about setting the properties. To do this, we simply use the object.property notation and set it to the value that we desire:

 objTelephone.Color = "Blue"
 objTelephone.Material = "Thermoplastic"
 objTelephone.Weight = 22
 objTelephone.NumberOfKeys = 12

As you can see, the general syntax for this is:

object.property = value

Lastly we set the object to nothing to release the reference and free up memory:

Set objTelephone = nothing

This is general housekeeping you should perform every time you have finished with an object. Now that we have set some property values in our telephone object, we can look at how to retrieve these values.

Continued…