gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:00:13 33 4
gpt4 key购买 nike

集合与 VBScript 中的字典有一些共同点,但它们不是同一件事。

集合是对象,例如 Session.Contents 属性。

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

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

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

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

Working with Collections

Using the Dictionary Class in VBA

我的问题是:

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

  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 循环始终为您提供类似集合的对象的 key 。然后您可以使用该键来访问该值。 (如果它为您提供了值,则将无法在 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 集合基于常规集合,但具有一些额外的魔力。例如,访问丢失的 key 不会引发错误。

What is the documentation of collection object?

微软做得非常出色,不仅放弃了 VB 6.0 文档,而且还让剩下的任何内容都无法找到。最接近的是 Collection Object in Office VBA 的文档.

否则有 IIS Session object 的文档,适用于经典 ASP。相关:Storing and Removing Data from the ASP Session Object .

关于vbscript - ASP 中使用 vbscript 的 Collection 对象到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44093685/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com