萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP Dictionary 物件完整指南:屬性、方法與陣列比較

為何選擇 Dictionary?

特性Dictionary陣列
索引方式鍵數字索引 (0-based)
動態調整大小自動需 ReDim Preserve
刪除項目後自動填補留下空洞或需手動搬移
多維支援否(可存物件/陣列模擬)是
內建方法豐富度高低
隨機存取效能O(1) 雜湊查找O(1) 索引存取
依內容查找Exists O(1)需迴圈 O(n)

建立與基本操作

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

' 新增鍵值對
dict.Add "name", "張三"
dict.Add "age", 25
dict.Add "skills", Array("ASP", "VB6", "SQL")

' 修改值
dict.Item("age") = 26
' 或
dict("age") = 26

' 讀取值
Response.Write "姓名: " & dict.Item("name") & "
" Response.Write "年齡: " & dict("age") & "
" ' 檢查鍵是否存在 If dict.Exists("email") Then Response.Write "Email: " & dict("email") Else Response.Write "無 Email 資料
" End If ' 取得所有鍵與值(回傳陣列) Dim keys, items, i keys = dict.Keys ' Array("name","age","skills") items = dict.Items ' Array("張三", 25, Array(...)) For i = 0 To UBound(keys) Response.Write keys(i) & " = " & items(i) & "
" Next ' 刪除單一鍵 dict.Remove "skills" ' 清空所有 dict.RemoveAll ' 數量 Response.Write "剩餘筆數: " & dict.Count %>

屬性與方法完整列表

屬性

屬性說明
CompareMode設定/回傳鍵比較模式:0=vbBinaryCompare(區分大小寫,預設)、1=vbTextCompare(不區分大小寫)。
Count唯讀,鍵值對數量。
Item(key)設定/回傳指定鍵的值。若鍵不存在讀取會自動建立(值為 Empty)。
Key(oldKey)將現有鍵 oldKey 重新命名為新鍵。

方法

方法語法說明
Adddict.Add key, item新增鍵值對。鍵重複會報錯。
Existsdict.Exists(key)回傳 Boolean,鍵是否存在。
Itemsdict.Items()回傳所有值的一維陣列。
Keysdict.Keys()回傳所有鍵的一維陣列。
Removedict.Remove(key)移除指定鍵及其值。
RemoveAlldict.RemoveAll()移除所有鍵值對。

實務應用範例

1. 快取資料庫查詢結果

Function GetCategoryName(catId)
  Static cache
  If IsEmpty(cache) Then Set cache = Server.CreateObject("Scripting.Dictionary")
  
  If cache.Exists(catId) Then
    GetCategoryName = cache(catId)
    Exit Function
  End If
  
  Dim rs, sql
  sql = "SELECT cat_name FROM category WHERE cat_id = " & catId
  Set rs = conn.Execute(sql)
  If Not rs.EOF Then
    cache(catId) = rs("cat_name")
    GetCategoryName = cache(catId)
  Else
    GetCategoryName = ""
  End If
  rs.Close
End Function

2. 表單欄位驗證規則集

Dim validators
Set validators = Server.CreateObject("Scripting.Dictionary")
validators.CompareMode = 1 ' 不區分大小寫

validators.Add "username", Array("required", "minlen:3", "maxlen:20", "alphanum")
validators.Add "email",    Array("required", "email")
validators.Add "password", Array("required", "minlen:8", "complexity")

' 驗證迴圈
Dim field, rules, rule
For Each field In validators.Keys
  rules = validators(field)
  For Each rule In rules
    ' 執行對應驗證邏輯...
  Next
Next

3. 模擬多維字典(值存 Dictionary)

Dim userProfiles
Set userProfiles = Server.CreateObject("Scripting.Dictionary")

Dim profile
Set profile = Server.CreateObject("Scripting.Dictionary")
profile.Add "name", "李四"
profile.Add "email", "li@example.com"
profile.Add "roles", Array("editor", "author")

userProfiles.Add "user_1001", profile

' 讀取巢狀值
Response.Write userProfiles("user_1001")("name") ' 李四

效能與記憶體注意事項

  • Dictionary 內部使用雜湊表,Add/Exists/Item 平均 O(1)。
  • 大量資料(>10萬筆)考慮改用資料庫暫存表或 Redis 等外部快取。
  • 物件變數用完務必 Set dict = Nothing 釋放 COM 參考計數。
  • Application/Session 範圍存放 Dictionary 時,注意多執行緒同步(ASP 單執行緒模型下通常無問題,但 Web Garden 需謹慎)。
copyright © 萬盛學電腦網 all rights reserved