Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What really is a Collection object in asp using vbscript?

The collection has something common with Dictionary in VBScript, but they are not the same thing.

Collection is object such as Session.Contents property.

Dictionary is an object created using the following code.

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

I want to find the differences and commons with these two type objects, in order to know how to use them.

I have found these useful articles, but still not clearly understand what a collection really is in asp using VBScript.

Working with Collections

Using the Dictionary Class in VBA

My questions are:

  1. Is there no Collection data type in VBScript? you can create a dictionary but you can not create a collection object.

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

     for each x in Session.Contents
    
  3. Why can you use collection this way?

     Session("FirstName")
    

    is the same as

     Session.Contents("FirstName")
    
  4. What is the documentation of collection object?

    The documentation of dictionary I can find is this

I may have misunderstood collection object, I so please tell me. Thank you so much.

like image 335
aruku7230 Avatar asked Mar 09 '23 01:03

aruku7230


1 Answers

Is there no Collection data type in VBScript?

Correct. There is no built-in Collection data type. It exists in VB, but VBScript does not have it. You can work with collections on existing objects in VBS, but you can't create new ones. Use the Scripting.Dictionary, which offers the same features.

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

For Each loops always give you the key on collection-like objects. You can then use the key to access the value. (If it gave you the value instead, there would be no way to get to the key in a for-each loop, i.e. you would have no idea what the value means.)

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")

Because the default property of the Session object is the Contents collection. And the default property of the collection objects is Item.

The default property is the one that gets called when you don't name a property in your code but still use parentheses on an object.

Therefore these are the effectively the same:

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>" )

Where the last variant is what actually happens while the other two variants are syntactic sugar. The Session.Contents collection is based on a regular collection, but has some added magic. For example, accessing missing keys does not throw an error.

What is the documentation of collection object?

Microsoft did a great job at not only abandoning the VB 6.0 documentation, but also making whatever remains of it impossible to find. The closest thing is the documentation of the Collection Object in Office VBA.

Otherwise there is the documentation of the IIS Session object, which applies to classic ASP. Related: Storing and Removing Data from the ASP Session Object.

like image 68
Tomalak Avatar answered Apr 27 '23 07:04

Tomalak