Sample Chapter: Beginning Active Server Pages 3.0 - Programming with Objects
(Page 4 of 8 )
To begin our look at programming with objects, let's use our trusty telephone object again. Being a technophile and always needing to have the latest and greatest, you have even hooked up your telephone to your computer. Now you want to be able to do something with it. If we want the computer to interact with the telephone, we need a programmatic object that will allow us to control the physical telephone.
It is this representation of a physical object that gives programmatic objects their power. Representation is literally the process of taking our real world object and turning it into a software object. In our telephone object example the color of the phone is represented by a color property, the weight of the phone is represented by a weight property. The ability to pick up the receiver and dial a friend is modeled by the PlaceCall method. The real world telephone has a set of features or characteristics that can be broken down into properties, methods and events. Once we have identified these features it makes it easier, to represent them in our software object.
However as described, in the previous section, the concept of encapsulation means that the actual workings of the telephone object are hidden from us. When a phone rings, you don't need to know how the signal was transmitted to the exchange, you only need to interact with the interface (talk into the handset). This is what we're going to consider with our software object, the interfaces we use to communicate with it, rather than the software object itself. So when we use our computer to control our object, it's not going to use the inner workings of the objects, rather it's going to communicate with the object and control it using its interfaces (the methods and events).
This book will not cover how to create the software object itself (you will be able to download this from the Wrox website – details of this are in the Try It Out coming up): rather, we will take a look at the programmatic object, and then look at how we can use the object's properties, methods, and events.
The Software Telephone Object So now we are shifting gears here from describing the real-world telephone as an object to describing a software telephone object. This is an example of representation. The properties of the telephone object are:
Property Name |
Color |
Material |
Weight |
NumberOfKeys |
TelephoneNumber |
Connected |
As you can see, we have used the same names that we used when discussing the physical telephone object. The methods of the telephone object are the same as well. In this case, the methods that have parameters will have the same parameters as well.
Method Name | Parameters |
PlaceCall | NumOutgoing |
Answer | No Parameters |
HangUp | No Parameters |
SendCardNumber | NumCCN, NumPIN |
Disconnect | No Parameters |
Finally – as you will expect by now – events that the object will support are the same events that are supported by the physical telephone object.
Event Name | Parameters |
IncomingCall | NumIncoming |
LineBusy | No Parameters |
CallWaiting | NumIncoming |
Now that we have defined the interfaces of our telephone object, we can take a look at some code examples that will show you how to use these interfaces. For these examples, we will be using VBScript, which is the language that is being used throughout the book. Since there are three types of interfaces, we will look at three code samples – one for each type.
Setting Up the Telephone Object Example We've supplied the telephone object on the Wrox web site. It comes as a DLL file, which has to be installed and registered before you can use it. The good news is that it is automatically installed and registered by running a self-contained exe file. Once you've run the file, you will have a telephone object, ready to include in the script of your ASP pages. Let's look at what needs to be done to install it.
Try It Out – Installing the Telephone DLL- Download the MyTelephone.exe file from the Wrox web site at http://www.wrox.com. You can also find full support with any problems encountered during the installation at this URL.
- Once the file has been downloaded, you can run it to expand all of the files into a temporary directory.
- Go to that temporary directory and run setup.exe to install the MyTelephone object. Click on OK on the first dialog to confirm installation, and then click the icon to proceed with installation.
Troubleshooting Problems The differences between each individual's computer, operating system and set up sometimes means that the software installs but may fail to register correctly. If you run any of the examples later in this chapter and encounter an error generated by a call to any of the methods, such as the PlaceCall method, then this will be the problem.
The problem is easy enough to rectify, all you need to do is manually register the DLL yourself. To do that, once you've run setup.exe you'll find that a file called MyTelephone.dll has been created. It's also a good idea to stop and restart the web application manager before you use the DLL in any examples. It can be placed in any directory - the only important thing is that the file be registered in that directory. To do that, there is a file (provided by the machine's operating system) called REGSVR32.EXE. If you run that file and pass the name of the .dll as a parameter (e.g. regsvr32 MyTelephone.dll), it will register the file in that location. The best way to do this is from the command prompt: copy the .dll file to the desired directory, go to that directory, and then run regsvr32 MyTelephone.dll. The OS should find regsvr32.exe since it is usually in the WinNT/System32 folder, which is in the default path.
If it's still not working then check that the machine you are actually installing the file on is actually the web server, and not just the machine with your browser on, and then look at the web site http://p2p.wrox.com for support if you still have problems.
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:
- 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:
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.
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.
Next: Retrieving a Property >>
More ASP Articles
More By Joe O'Donnell