Chapter 56: ASP Dictionary Object
ASP Dictionary Object.
This is not a separate lesson in the main W3Schools ASP tutorial flow, but it is one of the most common COM objects that every serious Classic ASP developer used (and many legacy Indian business/intranet/ERP/government systems still use in 2026).
I will explain it like your favorite teacher who actually used Scripting.Dictionary in production Classic ASP code almost every day from ~2003–2010 — slowly, clearly, with real working examples, good habits, common mistakes, security notes, performance tips, and the exact patterns you would see in real old-school codebases.
1. What is the Dictionary Object?
The Dictionary object is a COM component (ActiveX object) from the Microsoft Scripting Runtime library — exactly the same library that gives you FileSystemObject.
It is basically a key-value store (like a hash table / associative array / map / dictionary in other languages).
In VBScript (Classic ASP), you do not have built-in arrays with string keys or collections like modern languages — so Dictionary was the go-to solution for:
- Storing name-value pairs
- Caching lookup tables
- Building mini-databases in memory
- Counting occurrences
- Storing shopping cart items
- Temporary data before saving to real database
- Replacing multi-dimensional arrays
ProgID (how you create it):
|
0 1 2 3 4 5 6 |
Set dict = Server.CreateObject("Scripting.Dictionary") |
Key facts you must remember:
- Keys are case-insensitive by default (you can change this)
- Values can be anything (string, number, array, object, another Dictionary…)
- Very fast lookups (O(1) time)
- Can be iterated with For Each key In dict.Keys or For Each value In dict.Items
- Exists only during the current page request unless you store it in Session or Application
2. Basic Syntax & Most Important Methods/Properties
| Method / Property | What it does | Example |
|---|---|---|
| dict.Add key, value | Add a new key-value pair | dict.Add “color”, “red” |
| dict(key) = value | Add or overwrite value for key | dict(“color”) = “blue” |
| value = dict(key) | Read value by key | color = dict(“color”) |
| dict.Exists(key) | Check if key exists | If dict.Exists(“color”) Then … |
| dict.Remove(key) | Delete one key-value pair | dict.Remove(“color”) |
| dict.RemoveAll | Delete everything | dict.RemoveAll |
| dict.Count | Number of items | Response.Write dict.Count |
| dict.Keys | Returns array of all keys | For Each k In dict.Keys … Next |
| dict.Items | Returns array of all values | For Each v In dict.Items … Next |
| dict.CompareMode | Set key comparison (0=binary, 1=text) | dict.CompareMode = 1 (case-insensitive) |
3. Basic Example 1 – Simple Key-Value Store
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>Simple Dictionary Example</h2> <% Dim dict Set dict = Server.CreateObject("Scripting.Dictionary") ' Add items dict.Add "name", "Rahul Sharma" dict.Add "city", "Hyderabad" dict.Add "age", 27 dict.Add "isAdmin", True ' Read and display Response.Write "<p>Name: " & Server.HTMLEncode(dict("name")) & "</p>" Response.Write "<p>City: " & Server.HTMLEncode(dict("city")) & "</p>" Response.Write "<p>Age: " & dict("age") & "</p>" Response.Write "<p>Admin? " & dict("isAdmin") & "</p>" ' Check existence If dict.Exists("email") Then Response.Write "<p>Email found</p>" Else Response.Write "<p>No email stored</p>" End If ' Count items Response.Write "<p>Total items in dictionary: " & dict.Count & "</p>" Set dict = Nothing %> </body> </html> |
4. Real-World Example 2 – Shopping Cart (Very Common Pattern)
|
0 1 2 3 4 5 6 |
<!-- add_to_cart.asp --> <% Dim itemID, qty itemID = Request.QueryString("item") qty = Request.QueryString("qty") If itemID <> "" And IsNumeric(qty) Then qty = CInt(qty) Dim cart If IsEmpty(Session("Cart")) Then Set cart = Server.CreateObject("Scripting.Dictionary") Set Session("Cart") = cart Else Set cart = Session("Cart") End If ' Add or update quantity If cart.Exists(itemID) Then cart(itemID) = cart(itemID) + qty Else cart.Add itemID, qty End If End If Response.Redirect "cart.asp" %> <!-- cart.asp --> <% Dim cart, total, item, qty total = 0 If Not IsEmpty(Session("Cart")) Then Set cart = Session("Cart") %> <table border="1"> <tr><th>Item ID</th><th>Quantity</th><th>Price (dummy)</th></tr> <% For Each item In cart.Keys qty = cart(item) Dim price : price = 100 ' dummy per item total = total + qty * price Response.Write "<tr>" Response.Write "<td>" & Server.HTMLEncode(item) & "</td>" Response.Write "<td>" & qty & "</td>" Response.Write "<td>₹" & FormatNumber(qty * price, 2) & "</td>" Response.Write "</tr>" Next %> </table> <p><strong>Total items: <%= cart.Count %></strong></p> <p><strong>Grand total: ₹<%= FormatNumber(total, 2) %></strong></p> <% Else Response.Write "<p>Your cart is empty.</p>" End If %> |
5. Example 3 – Cache Lookup Table (Performance Win)
|
0 1 2 3 4 5 6 |
<!-- In global.asa or startup include --> <% Sub Application_OnStart Dim dict Set dict = Server.CreateObject("Scripting.Dictionary") dict.Add "1", "Biryani" dict.Add "2", "Haleem" dict.Add "3", "Irani Chai" dict.Add "4", "Double Ka Meetha" Application("FoodMenu") = dict End Sub %> <!-- Then in any page --> <% Dim menu, code code = "2" If Not Application("FoodMenu") Is Nothing Then Set menu = Application("FoodMenu") If menu.Exists(code) Then Response.Write "<p>Category #" & code & ": " & Server.HTMLEncode(menu(code)) & "</p>" End If End If %> |
→ Database hit once on app start → fast lookups forever.
6. Security & Best Practice Warnings (Critical in 2026)
- Never store sensitive data in Dictionary in Session or Application — anyone with access can read it
- Always use Exists(key) before reading — otherwise dict(“missing”) returns Empty (not error)
- Case-insensitive by default — dict(“Color”) = dict(“color”) → Change with dict.CompareMode = vbBinaryCompare (1 = text/case-insensitive, 0 = binary/case-sensitive)
- Very fast for lookups — much faster than arrays for large sets
- Can nest Dictionaries (value = another Dictionary)
- Clean up: Set dict = Nothing (though VBScript garbage collects)
7. Teacher Summary – ASP Dictionary Object in Classic ASP
ASP Dictionary Object (Scripting.Dictionary) is:
- Created with Server.CreateObject(“Scripting.Dictionary”)
- A key-value store (hash table / map / associative array)
- Keys are case-insensitive by default, values can be anything
- Main methods: .Add, .Item(key), .Exists, .Remove, .RemoveAll, .Count, .Keys, .Items
- Used for: shopping carts, lookup tables, caching, counting occurrences, temporary data
- Very fast lookups — perfect replacement for arrays when keys are strings
- Often stored in Session(“Cart”) or Application(“Cache”)
- Always check .Exists(key) before reading
- AlwaysServer.HTMLEncode keys/values when outputting to HTML
This is how millions of Classic ASP sites managed carts, cached menus, stored temporary data, and replaced ugly arrays — and many legacy Indian systems still use exactly this Dictionary pattern in 2026.
Next class?
- Want a full shopping cart example with Dictionary + add/remove/update?
- Or how to loop over keys and sort Dictionary items?
- Or nested Dictionaries (categories → products)?
- Or move to the next W3Schools topic (ASP TextStream or ASP Cookies)?
Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊
