网络编程
位置:首页>> 网络编程>> Asp编程>> ASP中遍历和操作Application对象的集合

ASP中遍历和操作Application对象的集合

作者:CODEOF  发布时间:2007-09-13 12:45:00 

标签:Application,global

为了能够正常使用,先把所提供的global.asa文件放到该应用程序的根目录中。最简单的办法是把global.asa文件放到缺省Web网站的根目录(缺省情况下是C:/InetPub/WWWRoot)中。
  
    对任何已有的global.asa文件重命名是一个好办法,可以在以后对该文件进行恢复。
  
    1. 显示Application集合的内容
  
    ASPCounter对象是StaticObjects集合的一个成员(通过<OBJECT>元素进行定义),但是其余部份(由Server.CreateObject实例化)是Contents集合的成员。
  
    可以看到使用global.asa例子网页放到这些集合中的值,这在前面已经看到:


 <!-- Declare instance of the ASPCounter component with 
  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 a” 
  varArray(1) = “Variant array” 
  varArray(2) = “stored in the” 
  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) 遍历Contents集合的代码
  
    为了遍历Contents集合,可使用一个For Each ... Next结构。集合中的每一项可以是一个简单的Variant类型变量、一个Variant数组或者一个对象的引用。因为需要对每种类型的值进行不同的处理,所以就不得不对每一个进行检查来判别其类型。
  
    在VBScript中可使用VarType函数完成这个工作。这里使用IsObject和IsArray函数代替:


For Each objItem in Application.Contents 
  If IsObject(Application.Contents(objItem)) Then 
  Response.Write “Object reference: ‘” & objItem & “’ 
  ” 
  ElseIf IsArray(Application.Contents(objItem)) Then 
  Response.Write “Array: ‘” & objItem & “’ contents are: 
  ” 
  VarArray = Application.Contents(objItem) 
  ‘Note: the following only works with a one-dimensional array 
  For intLoop = 0 To UBound(varArray) 
  Response.Write “ Index(“ & intLoop & “) = “ & _ 
  VarArray(intLoop) & “ 
  ” 
  Next 
  Else 
  Response.Write “Variable: ‘” & objItem & “’ = “ _ 
  & Application.Contents(objItem) & “ 
  ” 
  End If 
  Next 


 注意程序如何从Application对象检索该数组。将其分配给一个局部(Variant)变量,使用下面的语句: 
   

  varArray = Application.Contents(objItem) 


   
    使用UBound函数可以查找出数组的大小(元素的数量),这个值可以作为遍历的终止条件: 
   

  For intLoop = 0 UBound(varArray) 


   
    这个例子是一维数组,并将只显示这样的一个数组的内容。可根据需要编辑代码以处理多维数组,例如:

  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 “” 
  Next 


(2) 遍历StaticObjects集合的代码
  
    StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素声明的对象引用。因为每个条目都是一个对象变量,可用简单些的代码对这个数组进行遍历。我们将输出对象的名字(在ID属性中原有的定义):

For Each objItem in Application.StaticObjects 
  If IsObject(Application.StaticObjects(objItem)) Then 
  Response.Write “<OBJECT> element: ID=’” & objItem & “’ 
  ” 
  End If 
  Next 


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com