ASP collections Use

Source: Internet
Author: User
Tags arrays object end error code expression variables variable access
Collection | Collections Most ASP built-in objects support collections. Collections are places where strings, numbers, objects, and other values are stored. Apart from
Collections are automatically extended and searched when items are stored or retrieved, and collections are very similar to arrays. Unlike arrays,
When the collection is modified, the position of the item is moved. You can use the name of the item in the collection, the index, or the
All projects in a combined traversal Access project.
accessing items by name and index
You can access specific items in the collection by using the project name. For example, the Contents collection has a session
object to all variables saved in the You also have all the objects created by Server.CreateObject. Assume
The following user information is stored in the Session object:
You can access a project by using the name associated with it when you store the item in the collection. For example, the following expression returns a string
"Sam":
You can also access items by using the index or number associated with the project. For example, the following expression retrieves the type stored in the
The information in the second storage slot of the session object and returns "Woo":
The ASP collection is numbered starting from 1. When an item is added to or deleted from the collection, the index associated with the project changes.
Therefore, you cannot assume that the index of the item remains unchanged. As will be explained in the following topics, the use of indexed
Access is typically used to traverse a collection, or to access items in a read-only collection.
Items can be accessed by using shorthand names. The ASP searches for a collection associated with an object in a particular order. If
You can eliminate the name of a specific name in an object's collection if it appears only once:
When you access items stored in a application or Session object, it is generally safe to eliminate the collection name.
However, for the Request object, it is best to specify the collection name, because the collection is likely to contain duplicate names.
Traverse Collection
Iterate through all the items in the collection to understand the items stored in the collection or modify the project. When traversing a collection, you must
Provides the collection name. For example, you can use the for in VBScript ... Each statement accesses the stored in session
Items in the object:
You can use the for in VBScript ... Next statement to traverse the collection. For example, to list the examples stored in the
Three items in the session, you can use the following statements.
Because you generally do not know the number of items stored in the collection, ASP supports the Count property of the collection, which returns
Returns the number of items in the collection. You can use the Count property to specify the end value of a counter.
You can use a for statement in a script to loop through the collection. Use the count genus in a for statement for JScript
, you should assign the count value to a local variable and use the variable to set the count for greater effect.
End value of the device. This way, the script engine does not need to search for the value of Count for Each loop. The following example shows the
A tip:
Microsoft JScript 3.0 introduces the enumerator object. You can use this object to traverse an ASP collection.
The AtEnd method indicates whether the item still exists in the collection. The MoveNext method moves to the next item in the collection.
Traversal of a collection of key words (subkeys)
Scripts embed related values in a single cookie to reduce the number of cookies sent between the browser and the WEB server
Number. Therefore, the Request and Response object's Cookies collection can have multiple values in a single project.
These subprojects or sub keywords can be accessed individually. Only Request.Cookies and Response.Cookies.
The collection supports sub-keywords (subkeys). Request.Cookies only supports read operations; Response.Cookies only
Supports write operations.
You can enumerate all the cookies in the Request.cookie collection and all the child keys in the cookie
(subkeys). However, if you traverse a child keyword on a Cookie without a subkey (subkeys), you will not
will produce any results. by using. The HasKeys syntax first checks to see if the cookie contains a child keyword (subkeys).
Can avoid this situation. The following example shows this technique.
Traversing the collection of objects
The session and application collections can hold either a quantity variable or an object instance. Contents Collection Owning number
A measure variable and an object instance generated by calling Server.CreateObject. The StaticObjects collection has
The object created by using HTML elements in the Global.asa file. When you traverse a collection that contains objects, you can access the
The object's identifier or object's methods and properties. For example, suppose your application uses some objects to create a user account,
And each object has an initialization method that can traverse the StaticObjects collection to invoke each initialization method:
What is the difference between an ASP collection? Although the ASP collection discussed in this topic is related to the Visual basic
Collection objects are very similar, but they are still somewhat different. The ASP collection supports the Count property and the Item method.
The ADD and Remove methods are not supported. Setting the scope of an object scope object determines which scripts can enable
With the object. By default, when you create an object instance, the object has a page scope. In the same ASP page
Can use the object of the page scope, and when the ASP page is sent back to the client, the object is released
Put For most objects, the recommended scope is the page scope. You can change the scope of an object so that
It can be used by scripts from other pages. This topic explains how to use the object of the page scope and how to change the object's
Scope.
Objects created with Server.CreateObject on an ASP page using a page scope object are within the lifetime of the page
Always exist. The object is accessible to any script command on the page, and when the ASP finishes processing the page, the object
IS released. Therefore, an object has the scope or lifecycle of the page. In Visual Basic or VBScript
When you are programming, be careful not to release objects until the ASP has finished processing the page. For example, the following statement is often used to
Object variables are assigned the Nothing value to release the object: Set myobj = Nothing if you include in the ASP page
The statement, any attempt to use MyObj will return an expected error code. But in the interior, that
Enables the ASP to retain a reference to the object after it has been released. When you cannot use an object in a script, the object's resources
It is not released until the ASP has finished processing the page. Similarly, if you create another object instance and assign it to a used
Object variable to release the object, the ASP retains a reference to the original object instance. For most scripts,
Creating multiple objects may not cause problems, but if the object uses a shared resource, such as a database connection, there can be
can have problems. Because objects have page scopes, do not rely on objects that are manually freed. For example, the following loops
Create 1001 Connection objects that will open most connections and even a large SQL
Server: In general, you should try to avoid creating objects within a loop. If you can't avoid it, you should hand
The resource used by the object is released by the worker. If the Connection object is created only once, and the data resource's physical
The connection is opened in each loop and then closed, so the example will work: give the session scope to the object
In the application, the Session-scope object is created for each new session, and the session ends
to release it. Therefore, each active session has an object. The session scope is used to invoke from multiple scripts
object, but only one user session is affected. You can assign a session scope to an object only when it is needed. If you do
You need to use session scope, then you must understand the threading model of the component that provides the object because it affects performance and
The security environment for the object. For more information, see "Advanced Information: Performance Issues" in this topic. To assign to an object
To the session scope, store the object in an ASP sessions built-in object that you can use to Global.asa
File, you can also use the Server.CreateObject method on an ASP page to create a session
The object instance of the scope. In the Global.asa file, you can extend the RUNAT property (which must be set to
Sever) and the;object> tag for the SCOPE property (which must be set to session). The following example creates a
Session scope instance for Ad Rotator object:
Once you have stored the object in the Session object, you can access the object from any page in the application.
The following statement uses the object instance created by the tag in the previous example: on an ASP page, you can also use the
The Server.CreateObject method stores the object in a session-built object. The following example
object to store an instance of the Ad Rotator object. To display ads, you should first get the stored
An instance of the Ad Rotator object in the Session object before the method can be invoked to display the object: using the superscript
An ASP does not create an instance of a declared object until it is referenced by a script command in an. asp file.
The Server.CreateObject method creates the object instance immediately. Therefore, for session-scoped objects, the
It is better to use tags than server.createobject properties. To assign an application scope to an object
Applicatio

[1] [2] Next page



Related Article

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.