BEGINNING ACTIVE SERVER PAGES 3.0 PART 4 – CALLING METHODS OF AN OBJECT (Page 1)

There are two points that we need to be concerned about:

  • If the method requires parameters, that they are passed correctly.
  • If the method has a return value we must receive and capture it.

Try It Out – Calling a Basic Method

To make this example a simple one, we will be calling a method that has no parameters. Also, in this example, we are not interested in its return value. We will be using the same objTelephone instance of our telephone object that we have been using in the previous examples in this chapter.

1. Using your editor of choice, enter the following source code:

<%
Option Explicit
Dim objTelephone
Dim blnIsConnected

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

Response.Write "Answering the phone...<BR>"
objTelephone.Answer()

blnIsConnected = objTelephone.IsConnected
Response.Write "The IsConnected property is " & blnIsConnected & "<P>"

Response.Write "Hanging up the phone...<BR>"
objTelephone.HangUp()
Response.Write "The IsConnected property is " & _
objTelephone.IsConnected & "<P>"
Set objTelephone = nothing
%>

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

3. View the file in your web browser.

unnamed-file-276 BEGINNING ACTIVE SERVER PAGES 3.0 PART 4 - CALLING METHODS OF AN OBJECT
       (Page 1)

How It Works

In this example, we are using two of the methods that the Telephone object supports. We will also be checking one of the properties after calling the methods to see if they had any effect.

<%
Option Explicit
Dim objTelephone
Dim blnIsConnected
Set objTelephone = Server.CreateObject("MyTelephone.Telephone")

The first step, as we have done in the previous examples, is to create an instance of the Telephone object using the Server.CreateObject method. The reference that this method returns will be stored in a local variable. Remember that since we are storing a reference to an object, we have to use the Set statement.

Response.Write "Answering the phone...<BR>"
objTelephone.Answer()

The next step is to call the Answer method of the Telephone object. We will use the reference to the instance that we created to call the method. The preceding Response.Write line is being used to provide a visual indication that the method is being called.

blnIsConnected = objTelephone.IsConnected
Response.Write "The IsConnected property is " & blnIsConnected & "<P>"

Next, we will want to check the status of the IsConnected property. This property indicates if the phone is in use or not. Since we have just answered the phone, we would assume that this property would be set to true. We will store its value in a local variable, then use that local variable in a Response.Write method to display its value.

Response.Write "Hanging up the phone...<BR>"
objTelephone.HangUp()
Response.Write "The IsConnected property is " & _
objTelephone.IsConnected & "<P>"
Set objTelephone = nothing
%>

Finally, we will hang up the phone by calling the HangUp method of the Telephone object. Once that has completed, we will check the value of the IsConnected property again. This time, instead of storing the value of the property to a local variable before displaying it, we will directly display the value of the property. Both ways work exactly the same way. Then we can release the reference to the object.

Next, we will look at a variation of this example and see how to call a method that has a parameter.

Continued…