๐Ÿงฐ Common ASP References (Shared)
Estimated reading: 3 minutes 279 views

ASP โ€“ Dictionary Object โ€“ Store and Manage Key-Value Pairs in Classic ASP

Introduction โ€“ What Is the Dictionary Object in ASP?

In Classic ASP, the Scripting.Dictionary object is a powerful, server-side key-value store used to dynamically store, retrieve, and manage data pairs. It functions similarly to a hashtable or associative array and is often used for session-level data, caching, and quick lookups.

In this guide, youโ€™ll learn:

  • How to create and populate a Dictionary object
  • Use methods like Add, Remove, Exists, Keys, and Items
  • Iterate through dictionaries and use them in ASP pages
  • Best practices and output examples

Creating the Dictionary Object

<%
Dim dict
Set dict = Server.CreateObject("Scripting.Dictionary")
%>

Add Key-Value Pairs

<%
dict.Add "Username", "Vaibhav"
dict.Add "Role", "Admin"
%>

Keys must be unique, or it will throw an error.


Retrieve Value by Key

<%
Response.Write "User: " & dict.Item("Username")
%>

Output:
User: Vaibhav


Modify Existing Value

<%
dict.Item("Username") = "Alex"
Response.Write "Updated User: " & dict.Item("Username")
%>

Output:
Updated User: Alex


Check If a Key Exists

<%
If dict.Exists("Email") Then
    Response.Write "Email exists"
Else
    Response.Write "Email not found"
End If
%>

Remove a Key

<%
dict.Remove("Role")
%>

Remove All Entries

<%
dict.RemoveAll
%>

Iterate Over Dictionary

<%
Dim key
For Each key In dict.Keys
    Response.Write key & ": " & dict.Item(key) & "<br>"
Next
%>

Output:

Username: Alex

Full Example โ€“ User Info in Dictionary

<%
Dim userInfo
Set userInfo = Server.CreateObject("Scripting.Dictionary")

userInfo.Add "Name", "Priya"
userInfo.Add "Email", "priya@example.com"
userInfo.Add "Age", "28"

Dim k
For Each k In userInfo.Keys
    Response.Write k & ": " & userInfo.Item(k) & "<br>"
Next
%>

Output:

Name: Priya  
Email: priya@example.com  
Age: 28

Dictionary Object Methods & Properties

Method / PropertyDescription
.Add key, itemAdds a key-value pair
.Item(key)Gets or sets the value for a key
.Exists(key)Checks if a key exists
.Remove(key)Deletes a key-value pair
.RemoveAllClears all entries
.CountReturns number of elements
.Keys / .ItemsReturns all keys or values (arrays)

Summary โ€“ Recap & Next Steps

The Dictionary object in ASP is perfect for managing simple in-memory data structures like user data, session state, or application settings. Itโ€™s an efficient alternative to arrays when you need flexible key-based lookups.

Key Takeaways:

  • Use Scripting.Dictionary to create a key-value data store
  • Access or update items using .Item(key)
  • Use .Exists() to avoid runtime errors when checking keys

Real-world Use Cases:

  • Temporary in-memory caches
  • Collecting form data dynamically
  • Session-specific settings or mappings

FAQs โ€“ ASP Dictionary Object


What happens if I add a duplicate key?
It throws a runtime error. Use Exists(key) to check first.


Can I store Dictionary objects in Sessions?
Yes. Example: Session("UserData") = dict


Whatโ€™s the difference between a Dictionary and an Array?
Arrays use numeric indexes; Dictionaries use string-based keys and allow dynamic resizing.


Share Now :
Share

๐Ÿงพ ASP โ€“ Dictionary Object

Or Copy Link

CONTENTS
Scroll to Top