SunQuest
 
       ASP
  Home arrow ASP arrow Page 5 - Sample Chapter: Beginning Active Server Pa...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Sample Chapter: Beginning Active Server Pages 3.0
By: Joe O'Donnell
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2002-05-23

    Table of Contents:
  • Sample Chapter: Beginning Active Server Pages 3.0
  • Objects, Properties, Methods and Events
  • Return Values
  • Programming with Objects
  • Retrieving a Property
  • What is the Active Server Pages Object Model?
  • Using the Object Model as a Road Map
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Sample Chapter: Beginning Active Server Pages 3.0 - Retrieving a Property


    (Page 5 of 8 )

    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.asp, 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.asp, 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.

    Calling Methods of an Object
    The syntax for calling the 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 BegASP 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 source 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 BegASP 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.

    More ASP Articles
    More By Joe O'Donnell


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway