Create a variant array

Source: Internet
Author: User
Create a variant array
The method for creating and using a variant array to store values in the session and application objects is not discussed yet. It is discussed here as a very useful technology. As we can see, a variant data type can contain an array, not just a value.
An array stores a long row of binary values in a specified order in a continuous area of the memory. To arrange variant, You need to point to the first item and provide information about the size and structure. The script engine can do the rest.
You can create a dimension, two-dimensional, or multi-dimensional array in a variant variable, and then assign the array to an application. Program Layer or user session layer variables, and ensure that the entire array can be used in the corresponding place. Below Code Demonstrate the use technology of a simple one-dimensional array:
Dim vararray (3)
Vararray (0) = "This Is"
Vararray (1) = "variant array"
Vararray (2) = "stored in"
Vararray (3) = "Session Object"
Session ("variant_array") = vararray
3. When and when the application and session will start and end
I mentioned this when I introduced how ASP applications and sessions work. The basic terms are as follows:
? When the first user requests an application within the scope (that is, the default root directory of the Web site ), or, when an asp web page of a user-defined virtual application in a sub-directory of the website is started. Occurs before any user session is started.
? When a user initiates a session (if no active session exists) in the default application or an ASP Webpage in a virtual application for the first request ).
? When a user does not download an ASP Webpage within the specified timeout period, the session ends. The timeout value can be used in the script code. the timeout attribute can be set separately for each application in the Properties dialog box, or the default value in the IIS metadatabase can be partially modified through the IIS in Active Directory. After a webpage that calls the session. Abandon method is executed, the session also ends.
? The application ends immediately after the last active session in an application ends.
4. asp processing commands
As shown in chapter 1st, you can add a processing instruction to an asp web page. The processing command can contain more than one entry as needed. The keywords that can be used in the statement and their descriptions are shown in Table 3-10:
Table 3-10 keywords and descriptions of ASP commands
Command keyword
Description

Language = "language_name"
Set the default script language for the web page, for example: <% @ Language = "VBScript" %>

Enablesessionstate = "true" | "fasle"
When it is set to "true", the cookie of a session is prevented from being sent to the browser, so no new session object will be created, and the content of any existing session will no longer be available.

CodePage = "code_page"
Set the webpage code page, such as <% @ codePage = "1252" %>

Lcid = "locale_identifier"
Set the location identifier of the webpage, as shown in <% @ lcid = "2057" %>

Transaction = "transaction_type"
Indicates that the webpage file runs in a transaction environment. Valid values:
"Required": if a transaction is available, the script runs in it; if no transaction is available, start a new transaction.
"Requires_new": the script will initialize a new transaction.
"Supported": if a transaction is available, the script runs in it, and a new transaction is not started.
"Not_supported": the script will not run in any existing transactions and will not initialize a new transaction.
Describes transactions in detail in Chapter 1.

Only one processing command can be allowed on a web page, and it should be placed on the first line. The processing command can contain more than one such entry, but must be separated by spaces. There cannot be spaces at both ends of the equal sign. For example:
<% @ Language = "VBScript" codePage = "1252" lcid = "2057" %>

3.3.4 ASP application object of the activity
We provide some simple web pages that demonstrate ASP application and session objects during use. To work properly, you must put them in a virtual application on the server and put the provided global. Asa file in the root directory of the application. The simplest way is to put the global. Asa file in the root directory of the default web site (C:/inetpub/wwwroot by default.
Renaming any existing global. Asa file is a good way to restore it later.
All the example files in this book can be obtained from our Web site. In the chapter03 sub-directory of the example, there are all other sample webpages in this chapter.
In the chapter03 sub-directory, the default. asp web page is a simple menu that allows you to run the application and session sample web page, as shown in Figure 3-13:

Figure 3-13 default. asp running Screen
1. display the content of the application set
Click the first link to open the Application Object Example page named show_application.asp. It displays the content of the current application object of the virtual application, as shown in 3-14:

Figure 3-14 Application Object content Screen
Note that the aspcounter object is a member of the staticobjects set (defined by the <Object> element), but the rest (instantiated by server. Createobject) is a member of the contents set.
We can see that the global. Asa Example Web page is placed in these sets, as shown in the previous section:
<! -- Declare instance of the aspcounter component
Application-level scope // -->
<Object ID = "aspcounter" runat = "server" scope = "applicatoin"
Progid = "mswc. counters">
</Object>
...
...
<Script language = "VBScript" runat = "server">
Sub application_onstart ()
'Create an instance of An ADO connection with application-level scope
Set Application ("adoconnection") = server. Createobject ("ADODB. Connection ")
Dim vararray (3) 'create a variant array and fill it
Vararray (0) = "This Is"
Vararray (1) = "variant array"
Vararray (2) = "stored in"
Vararray (3) = "Application Object"
Application ("variant_array") = vararray 'store it in thd Application
Application ("start_time") = CSTR (now) 'store the date/time as a string
Application ("visit_count") = 0 'set counter variable to zero
End sub
...
...
</SCRIPT>
(1) code for traversing the contents set
To traverse the contents set, you can use a for each... Next structure. Each item in the set can be a simple variant type variable, a variant array, or an object reference. Because different processing needs to be performed on each type of value, we have to check each value to identify its type.
You can use the vartype function in VBScript to complete this task. Here, the isobject and isarray functions are used instead:
For each objitem in application. Contents
If isobject (application. Contents (objitem) then
Response. Write "Object Reference: '" & objitem & ''<br>"
Elseif isarray (application. Contents (objitem) then
Response. Write "array: '" & objitem & "'contents are: <br>"
Vararray = application. Contents (objitem)
'Note: The following only works with a one-dimenwing Array
For intloop = 0 to ubound (vararray)
Response. Write "index (" & intloop & ") =" & _
; Vararray (intloop) & "<br>"
Next
Else
Response. Write "variable: '" & objitem & ''=" _
; & Application. Contents (objitem) & "<br>"
End if
Next
Note how the program retrieves the array from the application object. Assign it to a local variable using the following statement:
Vararray = application. Contents (objitem)
You can use the ubound function to find the array size (number of elements). This value can be used as the termination condition for traversal:
For intloop = 0 ubound (vararray)
This example is a one-dimensional array, and only the content of such an array is displayed. You can edit the code as needed to process multi-dimensional arrays, for example:
For intloop = 0 to ubound (vararray)
Intnumberofdimensions = ubound (vararray, 1)
For intdimension = 0 to intnumberofdimensions
Response. Write "index (" & intloop & ") =" _
& Vararray (intloop, intdimension)
Next
Response. Write "<br>"
Next
(2) code for traversing the staticobjects set
The staticobjects set contains all object references declared using the <Object> element in global. Asa. Because each entry is an object variable, you can use simple code to traverse the array. We will output the Object Name (the original definition in the ID attribute ):
For each objitem in application. staticobjects
If isobject (application. staticobjects (objitem) then
Response. Write "<Object> element: Id = '" & objitem & "' <br>"
End if
Next

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.