asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

COM for ASP Programmers
By Wayne Berry
Rating: 3.4 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Overview

    If you are an Active Server page (ASP) developer, you have already used COM objects to create your ASP pages. However, unless you have developed COM objects or read a detailed book on COM, you might not understand COM enough to make use of the multitude of COM objects that are available to use in ASP. Also, without sufficient COM knowledge you might not be able to read some of the documentation that comes with other COM objects or infer the methods and properties that must exist. One of the wonderful things about COM is that once you learn the standards and the restrictions, you can use this knowledge to quickly learn how to implement other COM objects. In this Tutorial, we will try to explain how COM works from a Visual Scripter's reference point and give you the knowledge you need to master the world of COM.

    The Audience

    This tutorial is intended to demonstrate and describe the basics of the component object model to people that have used the Visual Basic Scripting language and COM, especially if you have used COM objects like ADO and didn't know that they are COM. This tutorial is for you if have ever wondered any of the following:

    1. What is the difference between a property and a method?
    2. Does a property take an argument?
    3. What does it mean for a property to be read only?
    4. What is a collection object?
    5. What properties are standard with every collection object?
    6. Why can I index a collection object without calling a method?
    7. How many COM objects can there be in a single DLL?

    The Basics

    COM is the standard for the Interface to objects. By definition COM Object only have methods and properties, there are not other interfaces. There isn't much difference between properties and methods from a programmer's standard point. Methods can take arguments, properties can't. Properties can be read/write, methods if they return a value, are read only.

    Even though programmatically methods and properties are not much different, component designers use methods and property for different functionality. Properties usually represent some aspect of the state of the object. Where as a method can be a function to do anything whether it involves the state of the object or not.

    Properties

    Properties do not take any arguments and are usually used to describe the state of the object or to set the state of an object. All properties return a value, however some properties are read-only, and some are read/write. Here is an example of the Visual Basic Scripting syntax for reading a property:

    Example 1

    
    value = object.property
    
    
    Notice there are no parenthesis, not even a blank set, i.e. (). Here is the Visual Basic syntax for setting a property:

    Example 2

    
    object.property = value
    
    

    Methods

    Methods can return values and take arguments, the are usually used to initiate an event within the object. Methods can be used to set values, but only when passing the value through the argument list. If a method returns a value but doesn't take an argument the syntax will be:

    Example 3

    
    value = object.method()
    
    
    Notice in example 3 that the method has a set of blank parenthesis. Methods that have a return value must have arguments encapsulated with parenthesis. For example, the Connection object has an execute method that returns a RecordSet Object. Here is an Example:

    Example 4

    
    Set RS = Conn.Execute("SELECT * FROM TABLE")
    
    
    Methods that do not return values, do not have parenthesis around the arguments. Such as the Close methods of the Connection Object:

    Example 5

    
    Conn.Close
    
    

    Arguments

    Methods can take one or more arguments, or take none at all. However, arguments might be optional, if they are, it is not required to enter anything for the argument. Once one argument is optional, all arguments following that are also optional. For example, if argument one and two are required, and three is optional, then argument four has to be optional too. A good example of an argument optional method is the Open method of the Connection object. The Open method has eight optional arguments. The first three are for establishing the database and the login information. You can call the Open method like in example 6:

    Example 6

    
    Conn.Open "DSN","sa",""
    
    
    To indicate a DSN of "DSN", a login of "sa", and a password of "". Or you can call the Open method like example 7:

    Example 7

    
    Conn.Open "driver=SQL Server;server=yourServerName;uid=someUID;" &_
    	"pwd=somePWD;database=someDatabase;"
    
    
    Notice that in example 6 we used three of the optional arguments and in example 6 we used only one with the same result.

    Calling the arguments by delimiting with and leaving the argument blank causes the method to be executed with nulls instead of the default value of the optional arguments. In example 6 the default value of the optional arguments is used, in example 8 nulls are used for the optional arguments.

    Example 8

    
    Conn.Open "DSN","sa","", , , ,
    
    
    Example 8 calls the optional methods with null values, which is different then example 6.

    Collections

    Collections are objects themselves that represent a set of objects. All collections have predefined methods and properties. A Collection has an Item method, a Count property and a _NewEnum method. A collection also by definition has the ability to create objects of the collection type. In other words, if a particular object can be group in a set then that object will have a collection object that can create an instance of an object within the set. For instance, a Drives collection object will contain a set of drives that might represent all the drives on a particular computer.

    The Count property returns a LONG value that specifies how many objects are in the collection. By passing a LONG value to the Item method, that is between one and the value returned by the Count property, the collection method will return the object in the set that is associated with that position. Accessing an item in an array works in similarly.

    The _NewEnum method allows the programmer to iterate through the collection in a For Next statement. Example 9 is an example of _NewEnum in action:

    Example 9

    
    For Each Object in Collection
    	 
    Next Object
    
    
    Notice that the _NewEnum method is not referenced within the syntax of the statement in example 6. This is because the _NewEnum method has a special index that Visual Basic recognizes as being used for the For Next statement. As a little background, all methods and properties in a COM object are indexed and certain indexes are used for particular tasks. For example the zero index is used for the default method or property.

    The Default Method or Property

    The method or property that has the COM index of zero is called the default property. Visual Basic allows the programmer to not use the regular method/property syntax when calling the default value, you can leave the syntactical call to the method/property off all together. For example, the default method in all collections is the Item method. If you where going to call the Item method, you could do it like it in example 9.

    Example 9

    
    Set Object = Collection.Item(2)
    
    
    This would get the second item in the collection and assign the object variable to that object. Because the Item method is the default method you can also call the Item method like in example 10:

    Example 10

    
    Set Object = Collection(2)
    
    
    Notice that both the period and the actual name of the method are missing, only the argument to the method remains.

    Instantiating an Object

    To create an instance of a COM object in ASP, you can use a statement like the one in example 11:

    Example 11

    
    Set Object Server.CreateObject("SMUM.XCheck.1")
    
    
    There is only one argument to the CreateObject method of Server that is the ProgId (the program ID). The ProgId is assigned by every component vendor to uniquely identify the COM Object. In order to create an instance of the COM object you must know the ProgId of the COM object.

    There is another way to get an instance of a COM object. You can have another COM object create the object and return the newly created object to you. This is how a collection works. You call the Item method of a collection and a COM object is returned that represents the subset of the collection which you index. Whenever a COM object is returned by another object, you must preface the statement with a Set like in example 12:

    Example 12

    
    Set Object = Collection.Item(2)
    
    
    Since Server is a COM object, example 11 and example 12 are much alike. They both return COM objects with a call to another COM object. The difference is that the CreateObject method of the Server object can return any COM object, the Item method can only return COM object that are stored in the collection. So if you need to have a COM object to create another COM object, where did the Server object come from? To solve the chicken vs. the egg problem there are a set of built-in COM objects in Active Server Pages.

    Built In COM Objects

    In the Active Server page environment there are six built in COM objects:

    • Server
    • Request
    • Response
    • ObjectContext
    • Application
    • Session
    The only different between these COM objects and all the others is that you do not need to create an instance of these COM objects in order to call them. Built in objects are present in the page without instantiation. They behave with all the other rules of COM and have their own methods and properties. Because these are built into the Active Server you do not need to know the ProgId, since you will not be calling the CreateObject method.

    Prog Id

    If one of the major ways to create a COM object is by using the CreateObject Method, then knowing the ProgIds of the objects you are creating is very important. So where are the ProgId located? The component vendor should be able to supply you with ProgIds for their components as part of the documentation.

    However, not all ProgIds are available, the vendor doesn't always want you to create an instant of the object using the CreateObject method. Some objects inherit properties from the object that creates them, so if they are not created from calling a method in that object then they are not initialized correctly. For this reason the vendor doesn't always supply the ProgId since he would like you to get the object from calling one of the methods from another of the vendors objects. For example, creating an instance of a ADO Field object would not do you much good without the going through the RecordSet object, the ADO Field object would not contain any data.

    The Documentation

    Now that we have established the understanding between methods and properties along with their different attributes, we need to understand how the documentation for the objects represents these attributes. For examples, we are going to look at 15 Seconds' component section, which is in the same format as the IIS 4.0 component documentation.

    Read and Write Properties

    A good example of a read/write property is that of the PhoneTranslate property of the XCheck object, shown here in example 11:

    Example 13

    object.PhoneTranslate[= value]
    Notice the value syntax, this is the indication of a property that can be written to. The brackets denote that the property is optional, in other words you do not need to set the property to use the object. Click here to view the full documentation.

    Read Only Properties

    A good example of a read only property is the Expires property of the ASPMail object.

    Example 14

    object.Expires
    Notice that unlike example 11 there is not an equal symbol, indicating this is read only. Click here to view the full documentation.

    Optional Method Arguments

    A good example of the optional arguments is the SendX method of the OCXMail object. The documentation syntax can be seen here in example 12:

    Example 12

    object.SendX(mailserver[, fromName[, fromAddress[, priority[, returnReceipt[, toAddressList[, ccAddressList[, bccAddressList[, attach[, messageSubject[, messageText]]]]]]]]]])
    Notice that the only required argument is the mailserver argument. All the rest, noted by the brackets are optional. Click here to view the full documentation.

    Summary

    With a fundamental understanding of COM and it's abilities, coupled with good documentation you can expand the flexibility of your Active Server page programming. Take the information that you already know about programming IIS objects, like Session objects and ADO, and expand on that by adding more COM objects to your repertoire. Third party COM object will allow you to expand your Active server applications and accomplish tasks rapidly by leveraging the component object model.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers