asp 中使用 vbscript 的 Collection 对象到底是什么?

What really is a Collection object in asp using vbscript?

该集合与 VBScript 中的 Dictionary 有一些共同点,但它们不是一回事。

集合是Session.Contents属性.

等对象

Dictionary 是使用以下代码创建的对象。

Dim dictObj
Set dictObj = CreateObject("Scripting.Dictionary")

我想找出这两种类型对象的区别和共同点,以便知道如何使用它们。

我找到了这些有用的文章,但仍然不清楚 asp 使用 VBScript 中的集合到底是什么。

Working with Collections

Using the Dictionary Class in VBA

我的问题是:

  1. VBScript 中没有集合数据类型吗?您可以创建字典,但不能创建集合对象。

  2. 当使用For Each循环遍历集合时,x是什么,集合的键还是集合项的值?

     for each x in Session.Contents
    
  3. 为什么可以这样使用集合?

     Session("FirstName")
    

    相同
     Session.Contents("FirstName")
    
  4. 集合对象的文档是什么?

    我能找到的字典文档是this

我可能理解错了集合对象,所以请告诉我。非常感谢。

Is there no Collection data type in VBScript?

正确。没有内置的 Collection 数据类型。它存在于 VB,但 VBScript 没有它。您可以使用 VBS 中现有对象的集合,但不能创建新对象。使用提供相同功能的 Scripting.Dictionary。

When loop through the collection using For Each, what is the x, the key of collection or the collection item value?

For Each 循环总是为您提供类似集合对象的密钥。然后您可以使用该键访问该值。 (如果它给你的是值,就没有办法在 for-each 循环中找到键,也就是说你不知道这个值是什么意思。)

Dim key, val

For Each key In Session.Contents
    val = Session(key)
    Response.Write Server.HtmlEncode(key) & " = " & Server.HtmlEncode(val) & "<br>"
Next

Why can you use collection this way?

Session("FirstName")

因为Session对象的默认属性Contents集合。而集合对象的默认属性是Item

默认 属性 是当您没有在代码中命名 属性 但仍然在对象上使用括号时调用的 属性。

因此这些实际上是相同的:

Session("FirstName") = "Foo"
Response.Write( Session("FirstName") & "<br>" )

Session.Contents("FirstName") = "Bar"
Response.Write( Session.Contents("FirstName") & "<br>" )

Session.Contents.Item("FirstName") = "Baz"
Response.Write( Session.Contents.Item("FirstName") & "<br>" )

最后一个变体是实际发生的,而其他两个变体是语法糖。 Session.Contents 集合基于常规集合,但增加了一些魔力。例如,访问丢失的键不会引发错误。

What is the documentation of collection object?

Microsoft 做得很好,不仅放弃了 VB 6.0 文档,而且还使它的任何剩余部分都无法找到。最接近的是 Collection Object in Office VBA.

的文档

否则还有IIS Session object, which applies to classic ASP. Related: Storing and Removing Data from the ASP Session Object的文档。