|
Beginning ASP 3.0

Altering the Properties of an Object
So, we have a telephone object, which defines the characteristics
of any telephone. For a particular instance of the object –
that is, a real physical telephone – values are associated to
the properties that describe the characteristics of that one
telephone.
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:
q Color
q Material
q Weight
q 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:
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.php, to your
BegASP directory.
3 View the file in your web browser. If everything worked properly,
then the browser will display the word Done.
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.
Retrieving a Property
The last section showed how to set the values of properties
of an object. Now that information is stored there, we can retrieve
this information at a later time. In essence, we have an instance
of an object that has some data stored in its properties. All
we need to refer to this instance is the reference to the object's
instance. All of the data that the object has stored inside
of it comes along with the object.
Read-Only Properties
In addition to the data that we have explicitly stored in the
object, there is information that the object uses to describe
its state. In our telephone object, there is a property called
Connected that describes whether or not a telephone is connected
to the telephone exchange. In order to change the connection
state of the phone, we would use a method. This method is read
only for the user, so the user cannot change it themselves.
The only way to change it is internally, as a result of using
a method such as PlaceCall, which would change from connected
from false to true.
You may wonder why we would not just change the property by
hand? This is another example of encapsulation. There is more
to disconnecting a phone than just changing a value of a property:
the object needs to perform some actions, which the user of
the object does not need to be concerned about. This functionality
is encapsulated in a method, and the method is responsible for
updating the value of the Connected property. This makes the
Connected property a read-only property, which means that we
cannot set its value, only retrieve it.
Try It Out – Retrieving Property Values
In this example, we will be retrieving the values of some of
the properties of the object, and storing them in local variables.
1 Using NotePad or your editor of choice, adapt the program
SetProperties.php, from above, as follows:
<%
Option Explicit
Dim objTelephone
Set objTelephone = Server.CreateObject("MyTelephone.Telephone")
objTelephone.Color = "Blue"
objTelephone.Material = "Thermoplastic"
objTelephone.Weight = 22
objTelephone.NumberOfKeys = 12
Dim strColor
Dim strMaterial
Dim intNumKeys
Dim intWeight
Dim blnConnected
strColor = objTelephone.Color
strMaterial = objTelephone.Material
intNumKeys = objTelephone.NumberOfKeys
intWeight = objTelephone.Weight
blnConnected = objTelephone.IsConnected
Response.Write "objTelephone.Color = " & strColor
& "<BR>"
Response.Write "objTelephone.Material = " &
strMaterial & "<BR>"
Response.Write "objTelephone.NumberOfKeys = " &
intNumKeys & "<BR>"
Response.Write "objTelephone.Weight = " & intWeight
& "<BR>"
Response.Write "objTelephone.IsConnected = " &
blnConnected & "<BR>"
Set objTelephone = nothing
%>
2 Save this code in the file RetrieveProperties.php, in the
BegASPFiles directory.
3 View the page in your browser.
How It Works
First, we set the Color, Material, NumberOfKeys and Weight properties
of our telephone, just as we did in the previous example. Next,
we allocate some variables that will hold the values of the
properties of our telephone object, using the Dim statement.
We allocate one variable for each property that we are storing:
Dim strColor
Dim strMaterial
Dim intNumKeys
Dim intWeight
Dim blnConnected
Next, we set about retrieving the property values. To do this,
we use the object.property notation again – this time to retrieve
the property, and then we store the property in the appropriate
variable. Here's the code that does this for the Color property:
strColor = objTelephone.Color
As you can see, the general syntax for this is:
myVariable = object.property
Then we output the results. Here's the line that does this
for the Color property:
Response.Write "objTelephone.Color = " & strColor
& "<BR>"
If the value of the property is a reference to an object, then
you will need to use the Set statement to assign the property
value to our local variable:
Set myVariable = object.property
We have now seen how to put information into the properties
of an object and retrieve that information. You might also have
noticed that the IsConnected property didn't return a value.
That's because it can only be set by another method, the Answer
method. As we haven't called the method, the value can't be
assigned. Up until the moment that the method is called, it
doesn't have a value. We'll be looking at how it can be changed
next. So now lets get our object to do some work for us by calling
its methods.
back | Contents
| Forward
|