| << 11.1.0- The Server Object | Chapter11 | 11.2.0- The Ad Rotator Component >> |
Creating an Instance of a Component
When you want to use a Server component, the first step is to create an instance of a component using the CreateObject method of the Server object.
You'll recall our discussion of the relationship between objects and instances from Chapter 5 . Essentially, the component (sometimes a class or type) is the template; and an instance is a particular copy that is created in the shape of that template. This action is similar to cookies coming out of a cookie cutter. The act of creating an object from a component is called instantiation.
In ASP, we first Dim a variable to hold a reference to the object. We then create an instance of the component with the Server.CreateObject method. This returns a reference to that object, which is assigned to our variable using the Set statement. The instance must be assigned to a variable name – this allows you to refer to the instance later on in your code. If we go by the naming convention outlines that we discussed previously, these are prefixed with obj before an abbreviation of the name of the component. For example, if you were using an instance of the Next Linker, then you might want to give it a variable name like objNextLink or objNL. Since the instance of your object is held in a variable, it should also be named in a similar manner. You may recall that we discussed naming conventions and rules in Chapter 4 .
A typical instantiation would look like the following piece of code:
<%
Dim objNL
Set objNL = Server.CreateObject("MSWC.NextLink")
...
'These lines can contain code that uses objNL
...
%>
We Dim the variable that will hold the instance of our object in the normal manner – avoiding special characters other then the underscore, starting with a letter and preferably starting with the obj abbreviation to designate an object. Then we use the Set statement, followed by the name that we have chosen for our instance. On the right of the equal sign is the code that we use to create an instance of the Server object's NextLink component. To give you the proper vocabulary, we are instantiating from a ("Library.Class"), or ("Vendor.Component"). (Class can sometimes be called ObjectType.) All of the Microsoft components that ship with ASP are in the Library "MSWC", standing for the Microsoft Web Class. Components from third parties or from other Microsoft software will have other class names.
Many students ask how to find out more about a given component. You can view all of the properties and methods of a class by using the Object Browser that comes with Microsoft Visual Basic and also with the VBA editors in Word, Excel and Access. (Of course, it must be a machine with IIS installed.) The techniques for VB and VBA Editors are a bit different; so pick the appropriate paragraph below.
To use the Object Browser in VB, you open a new project and click through Menu:Project/References, scroll down to MSWC (after "Microsoft") and you will see the ASP components listed, for example MSWC Advertisement Rotator Object Library and MSWC Content Linking Object Library. Click the corresponding checkboxes (to the left) to include those of interest, then close the References dialog box. Now click through Menu:View/ObjectBrowser or strike F2.
To use the Object Browser in one of the VBA Editors you start the application (Word, Excel or Access), then strike Alt+F11 to open the VBA editor. Click through Menu:Tools/References, scroll down to MSWC (after "Microsoft") and you will see the ASP components listed, for example MSWC Advertisement Rotator Object Library and MSWC Content Linking Object Library. Click the corresponding checkboxes (to the left) to include those of interest, then close the References dialog box. Now you can open the object browser by Menu:View/ObjectBrowser or striking F2.
Once the Object Browser is open, drop down the Project/Library list in the top left corner. Click on the component of interest and you can see its methods and properties in the pane to the right. Properties have an icon with a hand pointing to a list, methods have a flying green box and Events have a lightening bolt. By single clicking on a feature on the right you can get a short description and syntax guide in the gray pane below.
Common Instantiation Errors
- Forgetting the keyword Set
- Forgetting the equal sign
- Leaving off the obj prefix and ending up with a name that is a keyword
- Subsequently using a different name for the object
- Using unallowable characters in the object name
- Wrong ProgID or ClassType
- Errors in that pesky syntax of quotes-within-parenthesis. Remember that after Server.CreateObject we have a pair of parenthesis. Within the parenthesis we have a pair of double quotes. Within those double quotes we have the Library and Class separated by a period.
Now, we'll look at the specific techniques of using three components: the Ad Rotator, Content Linking, and Browser Capability.
| << 11.1.0- The Server Object | Chapter11 | 11.2.0- The Ad Rotator Component >> |

RSS
