COM programming model

Source: Internet
Author: User
Tags contains count documentation dsn expression iis object model
Overview
If you use ASP to do development, you may have used COM in your ASP page. However, before you develop a COM widget yourself or read a book detailing com, you probably don't fully understand COM, so you can't make the most of it to create your ASP page. Nor do you have a good understanding of the usage documentation that comes with COM parts. If you know the standards and limitations of COM, you can quickly learn to develop other COM parts.
In this tutorial, we will learn how COM works, and you will also learn to be proficient in COM.

Readers of this tutorial
This tutorial will give you a detailed description of the COM model for those who have already used the VBScript language, especially those who have used ADO but do not know that it is com. He will tell you:

1. The distinction between attributes and methods
2. Properties need parameters?
3. Read-only property what does it mean?
4. What is a collection object
5. What properties are there for each collection object
6. Do not call methods how to sort the collection
7. How many COM parts can be in a DLL.

Basic knowledge
COM is the standard for an object interface. Defining a COM object requires only defining methods and properties, and there are no other interfaces. From a programmer's point of view, there is not much difference between a property and a method. A method can take parameters, and a property cannot. properties can be read-write, and methods are read-only if they are to be returned.

Although the properties and methods are not much different from the programmatic perspective, the Widget developers use them to perform different functions. Attributes typically represent the state of an object, but the calling method can accomplish any task that it wants to accomplish, regardless of whether he contains the object's state or not.

Property
property does not require parameters to describe or set the state of an object. All properties return a value, some are read-only, and some are readable. The following is an example of an expression that reads a property in VBScript:

Example 1:

Value = Object.property


Note that there is no parentheses here. Example two is setting the property Example:

Example 2:

Object.property = value


Method
Method can have parameters and can return a value. The event that is typically used to initialize an object. When passing arguments to a method, the method can be used to set the value. If the method returns only a value and does not set a value, the expression is as follows:

Example 3:

Value = Object.Method ()


Note the parentheses in Example 3. You must use parentheses when calling a method to return a value. For example, the object connection has an Execute method that returns a Recordset object. Example:

Example 4:

Set RS = Conn.execute ("SELECT * from TABLE")


You do not need to return a value without using a parameter method, such as the connection object's Close method:

Example 5:

Conn.close


Parameters
Method can take one or more parameters, or none. However, parameters are not required. Once an argument is optional, subsequent arguments are optional. For example, parameter one and parameter two are required, and parameter three is optional. The parameter four must be optional. A good example is the open method of the Connection object. He has eight optional parameters. The first three are used to pass information about the database and the record. You can call the Open method as in Example 6:

Example 6:

Conn.Open "DSN", "sa", "" "


To provide a DSN name, the username, and the password is empty, you can also call the example 7:

Example 7:

Conn.Open "Driver=sql server;server=yourservername;uid=someuid;" &_
"Pwd=somepwd;database=somedatabase;"


Note that in example 6 we used three parameters, and in Example 7 we used only one, and the result was the same.

When the method is invoked, it is separated by commas, leaving the optional argument blank, passing null values to the parameter.
In Example 6, an optional parameter uses a default value, and a null value in example eight.

Example 8:

Conn.Open "DSN", "sa", "" ",,,,


Collection
A collection is a set of objects that itself contains many objects, and all collections contain predefined methods and properties. A collection has an Item method, a Count property, and a _NewEnum method. The collection has the ability to establish types with the same objects as him. In other words, If an object can be included in a set, then, alas, this sentence is very difficult, I do not turn, give the original. (In the other words, if a particular object can is group in a set then that object would have a collection object that can CRE Ate an instance of an object within the set. For instance, a drives collection object would contain a set of drives that might represent all the drives on a particular Computer).

The Count property returns a Long value that represents the number of elements in the collection. Passing a long integer to the Item method (of course, between 1 and count) returns the object that the index in the collection points to. Just like an array. (The text here is confusing, slightly adjusted)

Example 9 (1):

Set Object = Collection.item (2)

Because item is the default method, you can also invoke the following:

Example 9 (2):

Set Object = Collection (2)



The _NewEnum method can be called repeatedly,

Example 9:

For all Object in Collection

Next Object

(not translated below)
Notice that's the _NewEnum method was not referenced within the syntax of the
Statement in Example 6. This is because the _NewEnum method has a special
Index "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 the collections is the Item method. If you where going to call the Item
method, the could do it like it in Example 9.



To create a COM object in ASP, you can:

Example 11:

Set Object Server.CreateObject ("Smum. Xcheck.1 ")


The CreateObject method to the server passes only one parameter, an ID value, which is a symbol given by a COM part provider that uniquely identifies a COM object. To create an instance of a COM object, you must know the ID value of the object.

There is another way to get an instance of an object, you can create a new object instance with an existing object instance, and you actually work with the collection, you call the Item method, and you return an object instance.

Example 12:

Set Object = Collection.item (2)


For example 11 and example 12, one thing is the same, it's all about creating objects from other objects, except that CreateObject can create objects of any type, and item can only return objects in the collection. Like a chicken, or an egg, you might ask, How does the server object come from? In fact, this is a built-in object. He exists in the ASP.


Built-in objects
There are six built-in objects in the ASP, they are:
Server
Request
Response
ObjectContext
Application
Session
The only difference between these objects and other objects is that you do not need to create an instance. They have their own methods and properties as well as other objects. Because they are built in, so you don't need to know their IDs, in fact, you don't need to call CreateObject to create them.

Object ID
If the main way you create an object is to call CreateObject, it's important to know the ID of the object. COM part providers provide the ID of the object back in their document.

(not translated below)
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.
Seconds ' component section, which are in the same format as the IIS 4.0
Component documentation.
Read and Write Properties
A good example of a Read/write property is this of the Phonetranslate property of
The Xcheck object, shown in example 11:

Example 13
Object. phonetranslate[= value]
Notice The value syntax, this is the indication of the-a property so can be written to.
The brackets denote that's optional, in and words you don't need
To set the "to" use the object. Click here to view the full documentation.
Read only Properties
A good example of a read only-the Expires property of the Aspmail
Object.

Example 14
Object. Expires
Notice That unlike example there are not a 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 this in example 12:

Example 12
Object. Sendx (mailserver[, fromname[, fromaddress[, priority[,
returnreceipt[, toaddresslist[, ccaddresslist[, bccaddresslist[,
attach[, messagesubject[, MessageText]] []]]
Notice that's 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 sessions objects and ADO, and expand on so by adding more
COM objects to your repertoire. Third party COM object would allow you to
Expand your Active server applications and accomplish tasks rapidly by leveraging
The Component Object model.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.