Programming with Objects
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
1
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.
2 Once
the file has been downloaded, you can run it to expand all of the files into a
temporary directory.
3 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.