๐งพ 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
, andItems
- 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 / Property | Description |
---|---|
.Add key, item | Adds 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 |
.RemoveAll | Clears all entries |
.Count | Returns number of elements |
.Keys / .Items | Returns 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 :