HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
BEGINNING ACTIVE SERVER PAGES 3.0 PART 3 - ALTERING THE PROPERTIES OF AN OBJECT

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

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.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book .


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.

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
MICROSOFT VISUAL BASIC V6 ADVANCED - ORACLE BACKEND
To cover a series of advanced programming tasks and to fully command the VB programming language. Oracle is used ....
MICROSOFT VISUAL BASIC V6 ADVANCED - ACCESS BACKEND
To cover a series of advanced programming tasks and to fully command the VB programming language. Microsoft acces....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 4th December 2008  © COPYRIGHT 2008 - VISUALSOFT