| << 6.2.3- Altering the Properties of an Object | Chapter6 | 6.3.0- What is the Active Server Pages Object Model? >> |
Calling Methods of an Object
The syntax for calling a method of an object is very similar to setting or retrieving a property value. 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 BegASPFiles directory.
3. View the file in your web browser.
|
|
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.
Try It Out – Calling a Method with Parameters
In this example, we will be calling a method that has parameters – we're still not interested in the return value, just yet. Again, we will be using the objTelephone instance of our telephone object that we have been using in all the previous examples.
1. Using your editor of choice, enter the following code:
<%
Option Explicit
Dim objTelephone
Dim strPhoneNumber
Dim blnIsConnected
Set objTelephone = Server.CreateObject("MyTelephone.Telephone")
strPhoneNumber = "615-555-8329"
Response.Write "Calling " & strPhoneNumber & "...<P>"
objTelephone.PlaceCall(strPhoneNumber)
blnIsConnected = objTelephone.IsConnected
Response.Write "The IsConnected property is " & blnIsConnected & "<P>"
Set objTelephone = nothing
%>
2. Save this file, with the name ParameterExample.asp, to your BegASPFiles directory.
3. View the file in your web browser.
|
|
How It Works
Now we're telling the telephone to execute the PlaceCall method. As we know, the PlaceCall method doesn't work alone: we need to tell the telephone who to call! We do this by specifying the telephone number as a parameter to the PlaceCall method.
<%
Option Explicit
Dim objTelephone
Dim strPhoneNumber
Dim blnIsConnected
Set objTelephone = Server.CreateObject("MyTelephone.Telephone")
First, as we have done in the previous examples, we will create an instance of the Telephone object. The reference to this instance is then stored in a local variable.
strPhoneNumber = "615-555-8329"
Response.Write "Calling " & strPhoneNumber & "...<P>"
objTelephone.PlaceCall(strPhoneNumber)
The telephone number that we will be calling is stored as a string. In this example, the number is hard coded. We could have just as easily used a form to supply the value. We then will display a message indicating the number that will be called. We can then pass this value to the PlaceCall method. The parameter that we supply to the PlaceCall method is included within the method's parentheses. The contents of the parentheses are known as the parameter list. The entries in the parameter list could be variables or explicit values.
One thing that you need to be careful with is the order of the parameters in the parameter list. If we were calling a method that requires multiple parameters, then the order of the parameters in the parameter list must exactly match the order that the method is expecting. So, for example, if you call the SendCardNumber method, then you must specify two parameters: the first must be the value of the NumCCN parameter, and the second must be the value of the NumPIN parameter and these parameters are separated by a single comma.
Finally, we check the value of the IsConnected property and display its value to the user and release the object reference.
blnIsConnected = objTelephone.IsConnected
Response.Write "The IsConnected property is " & blnIsConnected & "<P>"
Set objTelephone = nothing
%>
We have now seen how to program with the properties and methods of objects. In our examples, we have been using an object that represents a physical entity. The remainder of this chapter will be devoted to looking at a set of objects that represent an application environment in Active Server Pages. These objects comprise the Active Server Pages object model.
| << 6.2.3- Altering the Properties of an Object | Chapter6 | 6.3.0- What is the Active Server Pages Object Model? >> |

RSS



